PHP file stream download method

Source: Internet
Author: User
Tags php download
How does PHP use a file stream to download a file ?? How does PHP use a file stream to download a file ?? 1. flush-refresh the output buffer. 2. ob_clean-clear (erase) the output buffer. note: The above two functions can solve the garbled code in the PHP download file. DEMO: & lt ;? Phpfname is the name of the file to be downloaded $ fpath is the folder where the downloaded file is located. the default value is downlodfunction PHP.

?

?

PHP file stream download method

?

?

1. flush-refresh the output buffer. 2. ob_clean-clear (erase) the output buffer. note: The above two functions can solve the garbled characters in the downloaded PHP files.


DEMO:


  Gbk $ filename = iconv ('utf-8', 'gb2312', $ fname); $ path = $ fpath. $ filename; if (! File_exists ($ path) {// check whether the echo file exists. "The file does not exist! "; Die () ;}$ fp = fopen ($ path, 'r'); // open $ filesize = filesize ($ path) in read-only mode ); // file size // returned File (stream form) header ("Content-type: application/octet-stream"); // return header ("Accept-Ranges: bytes "); // return the file size header (" Accept-Length: $ filesize "); // in the displayed dialog box on the client, the corresponding file name header (" Content-Disposition: attachment; filename = ". $ filename ); // ============================================================== ob_clean (); flush (); // ============================ ================/// Set the shunting $ buffer = 1024; // bytes counter for the next file $ count = 0; while (! Feof ($ fp) & ($ filesize-$ count> 0) {$ data = fread ($ fp, $ buffer); $ count + = $ data; // count echo $ data; // send data to the browser} fclose ($ fp);} download ("testfile.doc");?>

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.