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)