PHP download jpg file is empty workaround _php Tutorial

Source: Internet
Author: User
Tags php download
PHP tutorial Download jpg file as an empty solution

JPG format file, click Open in Browser to show as Red Fork

The code is as follows:

$filetype = "Image/pjpeg";
Header ("Content-type: {$filetype}");

Header ("expires:0");
Header ("Cache-control:must-revalidate, post-check=0,pre-check=0");
Header ("Pragma:public");

$file _content = file_get_contents ($filepath);
echo $file _content;
Exit

First troubleshoot the initial file if there is a problem

1, Echo $filepath;

2, according to the output file full path, open the file, no problem

The suspicion of excluding the original file

Then troubleshoot if there is a problem in the file read process

1, $filetype = "Application/jpeg";

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

Force a file to download

Download to find the file size is the same as the original file, but open or empty, you can determine the file output when there is a problem

With File_put_contents ("D:aaa.jpg", file_get_contents ($filepath)), save the file to the server, the saved file can be opened

At this point I suspect there was a problem encoding the file output to the client.

After a period of tossing, finally to solve the problem, the code is as follows:

Header ("Content-type: {$filetype}");
Header ("expires:0");
Header ("Cache-control:must-revalidate, post-check=0,pre-check=0");
Header ("Pragma:public");
Add encoding settings here
Header (' content-transfer-encoding:binary ');
The most important thing here is to empty the bytes of the output.
Ob_clean ();
This is equal to the two lines injected below, saving code
ReadFile ($filepath);

$file _content = file_get_contents ($filepath);
echo $file _content;
Exit

An explanation of Ob_clean () is attached here:

Clean (erase) the output buffer

I understand that ob_clean () is equal to Response.Clear () in. NET;

http://www.bkjia.com/PHPjc/632310.html www.bkjia.com true http://www.bkjia.com/PHPjc/632310.html techarticle php tutorial Download jpg file for empty solution jpg format file, click Open in Browser to display as Red Cross code as follows: $filetype = Image/pjpeg; Header (content-type: {$filetype} ...

  • 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.