如何强制下载图片文件

Pet*_*mas 2 php download

我正在尝试在 CI 中下载图像文件,但在打开下载图像文件时出现错误。需要帮忙 :(

$file_name = $_GET['file_name'];
            $file_path =  "./ups_printimages/".$file_name;
            if(file_exists($file_path))
            {
                $this->load->helper('download');
                $data = file_get_contents($file_path); // Read the file's contents
                $name = 'ups_label.png';
                force_download($name, $data);
            }
            else
            {
                echo "file does not exist";
            }
Run Code Online (Sandbox Code Playgroud)

Pet*_*mas 5

上帝。找到了解决方案:)

if(!file)
    {
         File doesn't exist, output error
         die('file not found');
    }
    else
    {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        ob_clean();
        flush();
        readfile($file);
        exit;
     }
Run Code Online (Sandbox Code Playgroud)