Why download pictures less than a few bytes

Source: Internet
Author: User
Tags ranges
$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);
  • Related Article

    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.