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