Detailed explanation of PHP implementation file download, _PHP tutorial

WBOY
Release: 2016-07-13 10:12:57
Original
989 people have browsed it

Detailed explanation of file download in PHP,

1. PHP download schematic

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, the php function cannot recognize the Chinese file name. Generally, if the program encoding is utf-8, the php function is relatively old and can only recognize gb2312 encoded Chinese, 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”);

4 Use header method to implement file download source code

Attached is the download method, which has been packaged and can be used directly:

Copy code The code is as follows:

Function download_by_path($path_name, $save_name){
          ob_end_clean();
            $hfile = fopen($path_name, "rb") or die("Can not find file: $path_namen");
Header("Content-type: application/octet-stream");
Header("Content-Transfer-Encoding: binary");
Header("Accept-Ranges: bytes");
Header("Content-Length: ".filesize($path_name));
Header("Content-Disposition: attachment; filename="$save_name"");
​​​​​while (!feof($hfile)) {
echo fread($hfile, 32768);
         }
             fclose($hfile);
}

The above is the entire content of this article. Do you guys have a preliminary understanding of how to implement file download in PHP? Try it yourself and combine it with the simple examples given in this article to make your project more perfect.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/918861.htmlTechArticleDetailed explanation of PHP implementation file download, 1. PHP download schematic diagram 2. File download source code: Copy the code as follows: php $file_name="haha.jpg";//File to be downloaded$file_name=iconv("ut...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template