What to do if the php downloaded image cannot be opened?

藏色散人
Release: 2023-03-04 11:58:02
Original
3101 people have browsed it

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.

What to do if the php downloaded image cannot be opened?

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);
}
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template