PHP implementation of the file download, the first need to pass the header () function to the Apache server to send some identification information, tell Apache to download the path of the file, name, type and other information, and finally use the file read and write function to read the contents of the file and output.
Take a look at the following 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
}else{
echo "file does not exist! ";
Exit
}
?>
The results of the operation are as follows:
Attention:
1, when the file is a binary stream, do not know the download file type, Content-type use Application/octet-stream
2, the Ob_clean () function is to empty the output buffer, if the function is not used, the file download after the photo does not open normally.
The above describes the PHP implementation of the file download, including the content, I hope the PHP tutorial interested in a friend helpful.