$file _name= "Koala.jpg";
if (!file_exists ($file _name)) {
echo "file does not exist";
Return
}
$FP =fopen ($file _name, "R");
$file _size=filesize ($file _name);
echo $file _size;
Header ("Content-type:application/octet-stream");
Header ("Accept-ranges:bytes");
Header ("Accept-length: $file _size");
Header ("content-disposition:attachment; Filename= ". $file _name);
$buffer = 1024;
while (!feof ($fp)) {
$file _data = fread ($fp, $buffer);
echo $file _data;
}
Fclose ($FP);
?>
Reply to discussion (solution)
_size the Echo $file;
Seemingly not, I this sentence is to write this sentence after the cancellation, in order to see the number of bytes to add later, the post of the time forget to remove the
Cause of Error:
You have opened the binary file in text mode
This problem picture processing often encounters to be used
$file = fopen ($file _name, "RB");
If you do not specify a "B" tag when working with binaries, you may encounter some strange problems, including broken picture files and strange questions about \ r \ n characters.
Thank you upstairs, I tried, it seems to be the same as the original, but thank you
As I did in the previous period of time, but also more than a few bytes, and then downloaded the picture opened failed
Here is the solution, looking for a long time to search
The code before (or after) has output, it may also be written to the downloaded file, so download a few more bytes
The solution to download a few more bytes is to use Ob_start (), and Ob_end_clean () to clear the previous output;
Ob_end_clean ();
Response headers required for HTTP download
Header ("Content-type:application/octet-stream"); The returned file
Header ("Accept-ranges:bytes"); Returns by byte size
Header ("Content-length: $file _size"); Return file size
Header ("content-disposition:attachment; Filename= ". $name);//The Client's popup dialog box, corresponding to the file name
Is it more than a few bytes, or a few bytes?
More than a few bytes can find the reason for the code, a few bytes should be the picture itself problem
The test is possible, I only commented Echo $file _size;
$file _name= "Maze.png", if (!file_exists ($file _name)) {echo "file does not exist"; return;} $FP =fopen ($file _name, "R"), $file _size=filesize ($file _name),//echo $file _size;header ("content-type:application/ Octet-stream "), Header (" Accept-ranges:bytes "), Header (" Accept-length: $file _size "), Header (" Content-disposition: Attachment Filename= ". $file _name); $buffer =1024;while (!feof ($fp)) {$file _data = fread ($fp, $buffer); Echo $file _data;} Fclose ($FP);