Why download pictures less than a few bytes

Source: Internet
Author: User
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);
  • 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.