This is a file download code, why the file is always empty after download
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 File
if (!file_exists ($file _path)) {
echo "File does not exist, sorry";
Return
}
$FP =fopen ($file _path, "R");
Get the size of the file to download
$file _size=filesize ($file _path);
Set the returned file
Header ("Content-type:application/octet-stream");
Sets the unit of return file, here is the byte
Header ("Accept-range:bytes");
Set the size of the returned file
Header ("Accept-length: $file _size");
Set the name of the file to be displayed when the Client popup dialog box
Header ("Content-disposition:attachment;filename= $file _name");
Loopback data to the client (sub-loopback)
Set the size of the loopback data each time
$buffer = 1024;
Set a file byte read counter to ensure the security of the download
$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 don't have echo file content
------Solution--------------------
$file _data=fread ($fp, $file _size);
You just read the data from the file.
But the data is not 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 of the fread return, and what is the use of it? +strlen ($file _data)