To download files in PHP, you first need to send some identification information to the Apache server through the header() function, telling Apache the path, name, type and other information of the file to be downloaded, and finally use the file reading and writing functions to read the file content and output it. .
Let’s look at an example:
$file = 'images/test.jpg';
if(is_file($file)) {
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=".basename($file));
ob_clean();
readfile($file);
exit; exit;
}
?>
The running results are as follows:
Note:
1. When the file is a binary stream and the downloaded file type is not known, the Content-Type uses application/octet-stream2. ob_clean() The function is to clear the output buffer. If this function is not used, the photo cannot be opened normally after the file is downloaded.
The above introduces the download of PHP implementation files, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.