Home >Backend Development >PHP Tutorial >PHP tutorial. Application example 7_PHP tutorial
PHP implements safe file download
The procedure is as follows:
$file_name = "info_check.exe";
$file_dir = "/public/www/download/";
if (!file_exists($file_dir . $file_name)) { //Check whether the file exists
echo "File not found";
exit;
} else {
$file = fopen($file_dir . $file_name,"r" ); //Open the file
// Enter the file tag
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header ("Accept-Length: ".filesize($file_dir . $file_name));
Header("Content-Disposition: attachment; filename=" . $file_name);
//Output file content
echo fread($file,filesize($file_dir . $file_name));
fclose($file);
exit;}
And if the file path is an "http" or "ftp" URL, the source The code will change slightly, the procedure is as follows:
$file_name = "info_check.exe";
$file_dir = "www.easycn.net/";
$file = @ fopen($file_dir . $file_name ,"r");