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");?>