使用Zend下载图像 - PHP标题:文件格式错误

时间:2011-06-21 01:40:38

标签: php header download

首先,我知道这个问题已被提出但无论如何我无法解决。 我需要设置下载图像的链接(jpg)。 我在这里和谷歌阅读了各种帖子,但结果总是如此: 我可以下载文件,但它仍然是相同的错误。 jpeg格式不正确。

  

Erreur d'interprétationdufichier   d'image JPEG(不是JPEG文件:启动   用0x0a 0x20)

当我在没有控制器的文件中测试时,没关系,但控制器中的脚本不起作用。

以下是测试代码:

 $file = '{document_root}/www/themes/default/images/common/background1.jpg';

if (file_exists($file))
{
    header('Content-type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . basename($file));
    header('Content-length: ' . filesize($file));

    readfile($file);
    exit;
}

此代码适用于简单的php文件。我下载图片,可以打开它。 但是在我的控制器中,文件不好。

我发现了标签?>可以添加空格,但我的控制器没有这个结束标记。 我已经使用各种帖子中的Zend对象测试了一些代码,但这是同样的错误。

我尝试过各种方法来读取文件(file_get_content(),fread()...),结果相同。 我认为我的Zend控制器出了问题。

我现在正在根据这篇文章测试我的文件: php file download: strange http header

任何线索都会非常感激。 谢谢你的帮助,抱歉我的英语不好。

[编辑:21/06/2011 - 6h38] 这是行动的代码

public function downloadAction()
{
    $this->view->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);

    $img = $this->_getParam('img');

    // Process the file
    $config = Zend_Registry::get('config');
    $width  = $config->catalog->image->original->maxWidth;
    $height = $config->catalog->image->original->maxHeight;

    $prefix = $width . 'x' . $height . '_';
    $filename  = $prefix . $img;
    $file = Zend_Registry::get('document_root') . '/data/images/catalog/products/' . $this->_getParam('pid') .'/'. $filename;

    if (file_exists($file))
    {
        $this->getResponse()
         ->setHeader('Content-Disposition', 'attachment; filename='.$filename)
         ->setHeader('Content-Transfer-Encoding', 'binary')
         ->setHeader('Content-Length', filesize($file))
         ->setHeader('Content-type', 'image/jpeg');

        $this->getResponse()->sendHeaders();

        readfile($file);
        exit;
    }

}

不直接调用此操作。我测试url中是否存在参数。 如果为true,则从listAction中调用downloadAction()。 我试图在两个动作中禁用视图和布局,但是有一些html呈现。

2 个答案:

答案 0 :(得分:1)

我在解密文件内容后发送内容时遇到同样的问题。 0x0a表示新行。您可能在?>之后有一些新行。在一些包含的类中标记。 放

ob_clean(); 
flush(); 

之前

readfile($file);

类似的东西:

header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-length: ' . filesize($file));
ob_clean(); 
flush();
readfile($file);
exit;

这对我来说很好。希望能帮助到你。 此致

答案 1 :(得分:0)

我编写工作zend框架:)

    public function dowloadfileAction(){
    $this->_helper->viewRenderer->setNoRender(true);
    $this->_helper->layout->disableLayout();
    if ($this->_user->isUserLogin()) { 
        $path_file = 'public/uploads/file/';
        $filename  = $this->_getParam('file');; // of course find the exact filename....
        $file = $path_file.$filename; 
        //zfdebug(mime_content_type($file)); die();
        if (file_exists($file)) {
        header('Pragma: public');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: private', false); // required for certain browsers 
        header('Content-Type: '.mime_content_type($file));
        header('Content-Disposition: attachment; filename="'. basename($file) . '";');
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: ' . filesize($file));
        readfile($file);
        }else{
            echo "File does not exist";
        }
    }else{
        echo "Please Login";
    }    
    exit;       
}
相关问题