PHP:强制从上面的目录root下载映像?

时间:2010-08-19 12:12:23

标签: php download jpeg

我正在尝试强制下载位于我的网站根目录上的目录中的图像。下载正常,并保存正确的文件名。但是,结束文件不是有效图像,不会打开或正确显示。这是我的代码:

        $photograph = new ViewPhotograph($photograph_id);
        $photograph->setPhotographVars();

        $file = $photograph->getPath('small');
        $filename = '1.jpg';

        header('Content-Description: File Transfer');
        header('Content-Type: image/jpg');
        header('Content-Disposition: attachment; filename=' . $filename);
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;

2 个答案:

答案 0 :(得分:3)

在调用ob_clean()

之前执行ob_start

一旦使用echo或类似物输出某些东西,你就无法摆脱这种情况,除非你在开始缓冲之前

答案 1 :(得分:0)

也许有一个PHP警告会破坏流。

注释掉标题,看看是否有警告。如果你这样做,修复它。 (请注意,您不应该display_errors启用生产服务器。)