1

我有一个接收参数的脚本,然后将文件下载到客户端浏览器。该文件不在文档根目录下可访问的文件夹中

当我的脚本执行 pdf 的工作正常但 word docents 没有。

我已经检查了带有显示符号的notepad ++文件,pdf和文档的顶部都有两行包含根据notepad ++的LF(看起来adobe reader更宽容)。

我已经检查了我能想到的一切。我尝试在输出之前将文件传递给修剪,在打开 php 标记之前检查没有空格,尝试将文件创建为循环每个元素的数组,如果它们包含“\n”,则不回显前两个,

这可能是apache设置问题吗?

示例脚本:

<?php
class TrainingDatabase_IndexController extends Zend_Controller_Action
{

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

            // Get filepath and decode it from get param
            $filepath = urldecode( $this->_request->getParam('directory') );

            // Return the basename for save as filename
            $filename = basename($filepath);


            // Set the headers
            header('Content-Disposition: attachment; filename="' . $filename . '"' );
            header('Content-Description: File Transfer');
            header('Content-Transfer-Encoding: binary');
            header('Content-Type: application/octet-stream');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Expires: 0');
            header('Content-Length: '.filesize($filepath));

            // Make sure nothing is left in the browser
            ob_clean();
            flush();

            // Output the file
            readfile($filepath);
   }
}
4

1 回答 1

0

I had checked every file used in the rendering of the page for closing php tags or whitespace before the opening php tags.

However I had not checked the custom Action Helpers I had created. I didn't even think to check this as I wasn't using them in this action.

These action helpers had closing php tags, having removed those it fixed the problem.

Thanks to everyone for your help! Ronnie

于 2013-07-08T10:00:07.883 回答