Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 强制下载时出现黑色图像_Php_Html - Fatal编程技术网

Php 强制下载时出现黑色图像

Php 强制下载时出现黑色图像,php,html,Php,Html,当我单击按钮时,浏览器会强制下载图像 代码工作,它下载的图像与正确的大小,但它都是黑色的 我正在使用这个代码: // get attachment location $attachment_location = "files/image.jpg"; if (file_exists($attachment_location)) { // attachment exists // send open/save jpg dialog to us

当我单击按钮时,浏览器会强制下载图像 代码工作,它下载的图像与正确的大小,但它都是黑色的 我正在使用这个代码:

    // get attachment location
    $attachment_location = "files/image.jpg";

    if (file_exists($attachment_location)) {
        // attachment exists
        // send open/save jpg dialog to user
        header('Cache-Control: public'); // needed for i.e.
        header('Content-Type: application/jpg');
        header('Content-Disposition: attachment; filename="image.jpg"');
        readfile($attachment_location);
        die(); // stop execution of further script because we are only outputting the jpg
        }
    else {
        die('Error: File not found.');
    }

我在我的测试服务器上尝试了你的代码。PHP5.3,Apache2.2,Win7 在Firefox和IE8中都可以正常工作

您确定问题不在于您的图像或您自己的浏览器吗? 您使用的是哪个版本的PHP?什么HTTP服务器?哪个操作系统?
您在哪个浏览器上测试了此功能?

是否可能缓冲区中还有其他数据?如果使用echo file\u get\u内容而不是readfile,会发生什么情况?如果你称ob_为clean,会发生什么;脸红在调用readfile之前


你有错误报告吗?在调用readfile或使用我上面提到的任何缓冲区操作之前,请尝试将其关闭。

我以为是应用程序/jpeg…@Mike D我以为是图像/jpeg;我不知道他以前有什么,但我认为是图像/jpeg-你不需要有一个内容长度的标题吗?不确定是否需要,我只是一直这么做。你能统计一下下载的图片和服务器上的图片,看看大小是否有差异吗?还有可能缓冲区中还有其他数据吗?如果使用echo file\u get\u内容而不是readfile,会发生什么情况?如果你称ob_为clean,会发生什么;脸红在调用readfile之前?只有正确大小和正确名称的黑色内容