In many website developments, users may need to download files. Using the header function provided by PHP, you can easily set the HTTP response header to control the download behavior of the file. In this tutorial, we will introduce how to use PHP's header function to set Content-Disposition to specify the file name of the downloaded file.
Content-Disposition is an HTTP header that tells the browser how to process files. The most common usage is to start the file download in the browser and specify a file name for the downloaded file. It is usually used with Content-Type , which tells the browser how to interpret the content type of the file.
The header function can be used to send raw HTTP headers. If we want to specify the file name when the file is downloaded, we can set the Content-Disposition header. Here is a simple example:
<?php
// Set file path
$file = 'path/to/your/file.txt';
// Make sure the file exists
if (file_exists($file)) {
// Set the correct content type,Tell the browser that this is a download file
header('Content-Type: application/octet-stream');
// set up Content-Disposition head,Specify the file name when the file is downloaded
header('Content-Disposition: attachment; filename="downloaded_file.txt"');
// set up文件大小
header('Content-Length: ' . filesize($file));
// Read and output file content
readfile($file);
exit;
} else {
echo "The file does not exist!";
}
?>
header('Content-Type: application/octet-stream'); : This line of code tells the browser to download a binary file and will not be automatically opened.
header('Content-Disposition: attachment; filename="downloaded_file.txt"'); : This line of code specifies that the file is downloaded as attachment (i.e. download), and the file is assigned the downloaded name "downloaded_file.txt" .
header('Content-Length: ' . filesize($file)); : This line of code specifies the size of the file, which helps the browser to correctly handle the download of the file.
readfile($file); : This function reads the file and sends the content to the browser, thereby starting the file download.
Sometimes, we might want the filename to be generated dynamically, not just a fixed string. In this case, the file name can be set through PHP variables. For example:
<?php
// Set file path
$file = 'path/to/your/file.txt';
// Dynamically generate file names
$fileName = 'downloaded_' . date('Y-m-d_H-i-s') . '.txt';
// Make sure the file exists
if (file_exists($file)) {
// Set the correct content type
header('Content-Type: application/octet-stream');
// 动态set up Content-Disposition head
header('Content-Disposition: attachment; filename="' . $fileName . '"');
// set up文件大小
header('Content-Length: ' . filesize($file));
// Read and output file content
readfile($file);
exit;
} else {
echo "The file does not exist!";
}
?>
In this example, the file name will contain the current date and time (such as: downloaded_2025-05-04_12-30-45.txt ), and the file name will be different each time you download it.
Sometimes, we need to provide file downloads through the URL, not just local files on the server. At this time, we can also use the header function to set Content-Disposition and download the file content from the remote server.
<?php
// set up远程文件 URL
$url = 'https://m66.net/files/sample.txt';
// Get the file content
$fileContent = file_get_contents($url);
// If the file content is successfully obtained
if ($fileContent !== false) {
// set up文件类型
header('Content-Type: application/octet-stream');
// set up文件下载名称
header('Content-Disposition: attachment; filename="sample_from_m66.txt"');
// set up文件大小
header('Content-Length: ' . strlen($fileContent));
// Output file content
echo $fileContent;
exit;
} else {
echo "Unable to download the file!";
}
?>
In this example, we download the file from https://m66.net/files/sample.txt and set the name of the download file through the header to sample_from_m66.txt .
Make sure there is no HTML output before sending any header . Because the HTTP header must be sent before any content is output.
If the file is large, consider streaming the file contents instead of loading the entire file into memory.
Through PHP's header function, we can easily control the download behavior of files, including setting the file name when the file is downloaded. In actual development, this function is very common, especially in applications such as file management systems and report generation.