PHP implementation of the file download function, the specific function code is as follows:
function Download ($file _url, $new _name= ") {
if (!isset ($file _url) | | Trim ($file _url) = = ") {
Return ' 500 ';
}
if (!file_exists ($file _url)) {//check if file exists
return ' 404 ';
}
$file _name=basename ($file _url);
$file _type=explode ('. ', $file _url);
$file _type= $file _type[count ($file _type)-1];
$file _name=trim ($new _name== ")? $file _name:urlencode ($new _name). $file _type;
$file _type=fopen ($file _url, ' R '); Open File
Input File Label
Header ("Content-type:application/octet-stream");
Header ("Accept-ranges:bytes");
Header ("Accept-length:". FileSize ($file _url));
Header ("content-disposition:attachment; Filename= ". $file _name);
Output file contents
Echo fread ($file _type,filesize ($file _url));
Fclose ($file _type);
}
Note that the above check file existence is through file_exists this function, but this function can only check the current server site directory inside the file, if it is the remote file on the Internet, you can use this site as follows: PHP to determine whether remote files exist method