Solution to the problem that the php downloaded image cannot be opened: first open the relevant PHP file; then add the "ob_clean" function to the file to discard the content in the output buffer.
Recommended: "PHP Video Tutorial"
php failed to download the image and open it!
I recently wrote a picture download. The download was successful. When I opened it, it said that the file was wrong and could not be opened. I searched for a long time.
Du Niang said it was a BOM problem, and then changed it. The encoding format is utf-8 without BOM format, and the problem persists after uploading.
Finally add ob_clean(); Problem Solving
The function of ob_clean is to discard the content in the output buffer. If your website has many generated image files, then think To access correctly, the buffer must be cleared frequently.
Here is my code:
function download($file_path,$file_name){ // header("Content-type:text/html;charset=utf-8"); //首先要判断给定的文件存在与否 if(!file_exists($file_path)){ return false; } $fp=fopen($file_path,"r"); ob_clean(); $file_size=filesize($file_path); //下载文件需要用到的头 Header("Content-type: application/octet-stream"); Header("Accept-Ranges: bytes"); Header("Accept-Length:".$file_size); Header("Content-Disposition: attachment; filename=".$file_name); $buffer=1024; $file_count=0; //向浏览器返回数据 while(!feof($fp) && $file_count<$file_size){ $file_con=fread($fp,$buffer); $file_count+=$buffer; echo $file_con; } fclose($fp); }
The above is the detailed content of What to do if the php downloaded image cannot be opened?. For more information, please follow other related articles on the PHP Chinese website!