PHP file stream download method (Appendix: solve the problem of garbled characters in the downloaded file content), _ PHP Tutorial

Source: Internet
Author: User
Tags php download
PHP uses the file stream download method (Appendix: solves the problem of garbled characters in the downloaded file content ),. PHP uses the file stream download method (Appendix: solves the problem of garbled characters in the downloaded file content). I remember I used a private game server in high school, and the game homepage was written in PHP at that time, because the file is very PHP, you can use the file stream download method (Appendix: solve the problem of garbled characters in the downloaded file content ),

I remember that I used private game servers in high school. at that time, the game homepage was written in PHP. because the files were fixed, the client, the login device, and some gadgets, the number of files was not large, therefore, the download link is directly written to the file directory of the local server. Today, a friend in the QQ group suddenly asked me to use the file stream for PHP download, I wrote a small Demo. the code is simple and the comments are complete. just go to the code ~

1. flush-refresh the output buffer

2. ob_clean-clear (erase) output buffer

This function is used to discard the content in the output buffer.

This function will not destroy the output buffer, but the function like ob_end_clean () will destroy the output buffer.

Note: The above two functions can solve the problem of garbled characters in PHP download files.

 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 ("cgexscript example .doc");?>




Ghost (attachment: solves the problem of garbled characters in the downloaded file content). I remember that I used a private game server in high school. at that time, the game homepage was written in PHP, because the file is very...

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.