A brief analysis of PHP file downloading principles,
1. PHP download schematic diagram
2. File download source code:
Copy code The code is as follows:
$file_name="haha.jpg";//File to be downloaded
$file_name=iconv("utf-8","gb2312","$file_name");
$fp=fopen($file_name,"r+");//To download a file, you must first open the file and write it into the memory
if(!file_exists($file_name)){//Determine whether the file exists
echo "File does not exist";
exit();
}
$file_size=filesize("a.jpg");//Judge file size
//Returned file
Header("Content-type: application/octet-stream");
//Return in byte format
Header("Accept-Ranges: bytes");
//Return file size
Header("Accept-Length: ".$file_size);
//Pop up the client dialog box, corresponding file name
Header("Content-Disposition: attachment; filename=".$file_name);
//Prevent the server from increasing instantaneous pressure and read in segments
$buffer=1024;
while(!feof($fp)){
$file_data=fread($fp,$buffer);
echo $file_data;
}
//Close file
fclose($fp);
?>
3. Solution to file encoding problem:
If the file name is Chinese, PHP's function cannot recognize the Chinese file name. Generally, if the program encoding is UTF-8, PHP's function is relatively old and can only recognize Chinese encoded by gb2312, so use iconv("original encoding" for Chinese). "Encoding to be converted to", "String to be transcoded") function can be transcoded.
For example, convert a string from utf-8 to gb2312
$file_name=iconv(“utf-8”,”gb2312”,”$file_name”);
http://www.bkjia.com/PHPjc/932484.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/932484.htmlTechArticleA brief analysis of the PHP file downloading principle, 1. PHP downloading principle diagram 2. File downloading source code: Copy the code as follows: php $file_name="haha.jpg";//The file to be downloaded $file_name=iconv("utf...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn