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:
- $ 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, 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:
- 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 part here is to clear the output bytes.
- Ob_clean ();
- // This is equal to the two injected lines below, saving code
- Readfile ($ filepath );
- // $ File_content = file_get_contents ($ filepath );
- // Echo $ file_content;
- 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 ();