650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20160614/3654979b25bb41eda917f5fa5b795bab_th.png "style=" border:0px;margin:0px;padding:0px;font-size:0px; "alt=" 3654979b25bb41eda917f5fa5b795bab_th.png "/>
How PHP Implements file downloads
1. Set the href attribute of the hyperlink
<ahref= "File Address" ></a>
If the browser cannot parse the file, the browser will download it automatically. If the file is a picture or TXT, it will open directly in the browser.
2. Output file stream
download.php
Called when the page is loaded
DownloadFile ("3.rar", "Something.rar");
$filePath is the file address of the server
$saveAsFileName is the user-specified file name after the download
function DownloadFile ($filePath, $saveAsFileName) {
Empties the buffer and turns off the output buffer
Ob_end_clean ();
R: Open as read-only, B: Force binary mode
$fileHandle =fopen ($filePath, "RB");
if ($fileHandle ===false) {
echo "Can not find file: $filePath \ n";
Exit
}
Header ("Content-type:application/octet-stream");
Header ("Content-transfer-encoding:binary");
Header ("Accept-ranges:bytes");
Header ("Content-length:". FileSize ($filePath));
Header ("content-disposition:attachment; Filename=\ "$saveAsFileName \" ");
while (!feof ($fileHandle)) {
Reads up to length bytes from a file pointer handle
Echo fread ($fileHandle, 32768);
}
Fclose ($fileHandle);
}
Hangzhou PHP Engineer Note:
(1) download.php can be set to the href attribute of <a> tag, click <a> Tag, then the browser will prompt to download.
(2) The click Method of the HTML object should be used when there is a bug in the jquery simulation trigger <a> Click event. $ (' #hyperLink ') [0].click ();
(3) JQuery Mobile will change the behavior of <a>. Therefore, when using jquery mobile, whether manually clicked or Java analog Click, will jump to the download.php page, and will not trigger the download. (4) Location.href or location.replace directed to download.php can also be downloaded. This approach is not affected by jquery mobile.
(5) When downloading the above two methods, Chrome will prompt "Resource interpreted as Document but transferred with MIME type Application/octet-stream". Adding HTML5 attribute download for <a> can solve this problem. <a href= "..." download></a> and location.href or location.replace triggered download, there is no way to solve.
This article is from the "Programmer's Caprice" blog, please be sure to keep this source http://cxykz.blog.51cto.com/11696626/1789112
How PHP Implements file downloads