search
HomeBackend DevelopmentPHP ProblemWhat to do if the php downloaded image cannot be opened?

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);
}

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment