[PHP] PHP implementation file download

Source: Internet
Author: User
Tags rar

1. Set the href attribute of the hyperlink
<a href="文件地址"></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.phpWhen the page loads, it calls DownloadFile ("3.rar","Something.rar");$filePath is the file address of the server$saveAsFileName is the user-specified file name after the downloadfunctionDownloadFile($filePath,$saveAsFileName) {Empties the buffer and closes 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 the file pointer handle echo fread ( $fileHandle, 32768); } fclose ( $fileHandle);           

Note:
(1) download.php can be set to <a> the label's href properties, click on the <a> label, the browser will prompt to download.
(2) jquery simulates <a> click a bug that triggers an event and should use the click method of the HTML object.$(‘#hyperLink‘)[0].click();
(3) JQuery Mobile will change <a> the behavior. Therefore, when using jquery mobile, whether manually clicked or JavaScript 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 ".
To <a> increase the HTML5 attribute download this problem can be solved.<a href="..." download></a>
and location.href or location.replace trigger the download, there is no way to solve.

[PHP] PHP implementation file download

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.