Why download pictures less than a few bytes
$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);
?>
------to solve the idea----------------------
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.
------to solve the idea----------------------
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
------to solve the idea----------------------
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);