我必须触发下载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);`
这正在我的预期核心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 is reading file instead of downloading.这个问题没有任何公认的答案(他在询问核心php,但我只有zend的问题).我尝试了所有这些,但它没有用.
尝试3:Changing the .htaccess.之后我认为这是我的.htaccess的一个问题,并找到了更改.htaccess文件的答案.
ForceType application/octet-stream
Header set Content-Disposition attachment
这也给了我相同的结果.
尝试4:Using download functions in Zend.我在这个问题的答案中尝试了所有的zend函数.但是给我一个空输出,即使文件没有被读取.
尝试5:按照answer删除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"]);
}
}