readfile()函数读取zip文件而不是下载它(Zend)

Pri*_*ath 9 php .htaccess zend-framework

我必须触发一个zip文件的下载(Zip文件在我的数据文件夹中).为此,我正在使用代码,

$file = 'D:\php7\htdocs\Project\trunk\api\data\file.zip';
header('Content-Description: File Transfer');
header('Content-type: application/zip');
header('Content-disposition: attachment; filename=' . basename($file) );
readfile($file);`
Run Code Online (Sandbox Code Playgroud)

这正在我的预期核心PHP中工作.但是当我在Zend中使用相同的代码时,打印出如下内容,

PKYsVJ)~?? study.xlsPKYsVJs?????+ tutorial-point-Export.xlsPKYsVJn??? 8??Zabc.xlsP

在内容之间我可以看到zip中所有文件的名称.但它没有下载.

在我意识到这不起作用之后我开始搜索它并从堆栈流程中找到一些解决方案

尝试1:ob在每个随机行中添加不同的标题元素和函数

  • 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: ' . $file_size);
  • ob_start();
  • ob_clean();
  • flush();

所有这些都是从不同的堆栈溢出问题和答案尝试并具有相同的结果

尝试2:PHP正在读取文件而不是下载.这个问题没有任何公认的答案(他在询问核心php,但我只有zend的问题).我尝试了所有这些,但它没有用.

尝试3:更改.htaccess.之后我认为这是我的.htaccess的一个问题,并找到了更改.htaccess文件的答案.

<FilesMatch "\.(?i:zip)$">
        ForceType application/octet-stream
        Header set Content-Disposition attachment
</FilesMatch>
Run Code Online (Sandbox Code Playgroud)

这也给了我相同的结果.

尝试4:在Zend中使用下载功能.我在这个问题的答案中尝试了所有的zend函数.但是给我一个空输出,即使文件没有被读取.

尝试5:根据答案删除php标记之前和之后的所有不需要的空格

有没有其他方法可以在ZF2框架中触发下载?

编辑

以下是我的确切功能.这是GET(API)函数,

public function getList(){
    try{
       //here i am getting the zip file name.
       $exportFile = $this->getRequest()->getQuery('exportid','');
       $file = 'D:\php7\htdocs\Project\trunk\api\data\\' . $exportFile . '.zip';
       header('Content-Description: File Transfer');
       header('Content-type: application/zip');
       header('Content-disposition: attachment; filename=' . basename($file) );
       readfile($file);
       return new JsonModel(["status"=>"Success"]);
    } catch(\Exception $e){
       return new JsonModel(["status"=>"Failed"]);
    }
}
Run Code Online (Sandbox Code Playgroud)

Ris*_*abh -1

您可以尝试使用此方法来下载文件而不是 readfile(); 服务器端 - file_put_contents("file.zip", fopen("http://someurl/file.zip", 'r'));

客户端 - <a href="http://someurl/file.zip"><button>download file</button></a> download file