Php to download jpg files is empty solution

Source: Internet
Author: User
In jpg format, click to open the file in the browser and display it as a Red Cross: The code is as follows: $ filetype & quot; imagepjpeg & quot; header (& quot; content-type: {$ filetype} & quot;); header (& quot; expires: 0 & quot;

In jpg format, click to open the file in the browser and display it as a Red Cross: The code is as follows:

  1. $ Filetype = "image/pjpeg ";
  2. Header ("content-type: {$ filetype }");
  3. Header ("expires: 0 ");
  4. Header ("cache-control: must-revalidate, post-check = 0, pre-check = 0 ");
  5. Header ("pragma: public ");
  6. $ File_content = file_get_contents ($ filepath );
  7. Echo $ file_content;
  8. Exit;

First, check whether the initial file is faulty:

1. echo $ filepath;

2. open the file based on the complete path of the output file. No problem

Exclude the suspicion of the original file,Check whether there are any problems during file reading:

1. $ filetype = "application/jpeg ";

Header ("content-disposition: attachment; filename =" {$ headername }";");

Force download of the file. after downloading the file, it is found that the file size is the same as that of the original file, but it is still blank after it is opened. you can determine that a problem occurs during file output. use

File_put_contents ("d: aaa.jpg", file_get_contents ($ filepath), save the file to the server, save the file to open, at this time, I suspect that a problem occurs when the file is output to the client. after some time, I finally solve the problem. The code is as follows:

  1. Header ("content-type: {$ filetype }");
  2. Header ("expires: 0 ");
  3. Header ("cache-control: must-revalidate, post-check = 0, pre-check = 0 ");
  4. Header ("pragma: public ");
  5. // Add encoding settings here
  6. Header ('content-transfer-encoding: binary ');
  7. // The most important part here is to clear the output bytes.
  8. Ob_clean ();
  9. // This is equal to the two injected lines below, saving code
  10. Readfile ($ filepath );
  11. // $ File_content = file_get_contents ($ filepath );
  12. // Echo $ file_content;
  13. Exit;

Here is an explanation of ob_clean:Clean (erase) the output buffer

I understand that ob_clean () is equal to response. clear () in. net ();


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.