This is a piece of file download code. why is the downloaded file always empty? Solution

Source: Internet
Author: User
This is a piece of file download code. why is the downloaded file always empty ;? Php & nbsp; functiondown_file ($ file_name, $ file_sub_dir) {& nbsp; $ file_name = iconv (& quot; UTF-8 & quot; this is the code for downloading a file, why is the downloaded file always empty?
Function down_file ($ file_name, $ file_sub_dir ){
$ File_name = iconv ("UTF-8", "gb2312", $ file_name );
$ File_path = $ _ SERVER ['document _ root']. $ file_sub_dir. $ file_name;
// Open the file
If (! File_exists ($ file_path )){
Echo "the file does not exist. sorry ";
Return;
}
$ Fp = fopen ($ file_path, "r ");
// Obtain the size of the object to be downloaded
$ File_size = filesize ($ file_path );
// Set the returned file
Header ("Content-type: application/octet-stream ");
// Set the unit of the returned object, in bytes
Header ("Accept-Range: bytes ");
// Set the size of the returned file
Header ("Accept-Length: $ file_size ");
// Set the file name displayed in the client pop-up dialog box
Header ("Content-Disposition: attachment; filename = $ file_name ");
// Send data back to the client (multiple delivery)
// Set the size of each send-back data
$ Buffer = 1024;
// Set a file byte read counter to ensure download security
$ File_count = 0;
While ((! Feof ($ fp) & ($ file_size-$ file_count> 0 )){
$ File_data = fread ($ fp, $ file_size );
$ File_count + = $ buffer;
}
Fclose ($ fp );
}

Down_file ("Sunset.jpg", "/filedown/down /");
?>

------ Solution --------------------
You do not have the echo file content
------ Solution --------------------
$ File_data = fread ($ fp, $ file_size );
You only read data from the file
But no data is output
Echo $ file_data;


------ Solution --------------------
PHP code
While ((! Feof ($ fp) & ($ file_size-$ file_count> 0) {$ file_data = fread ($ fp, $ file_size); $ file_count + = $ buffer ;} [B] print ($ file_data); [/B]……
------ Solution --------------------
$ File_data = fread ($ fp, $ file_size );
$ File_count + = $ buffer;

What is this? $ buffer is not the length returned by fread. what is the use of it? + Strlen ($ file_data)

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.