How PHP Implements file downloads

Source: Internet
Author: User
Tags rar

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.