php – readfile()函数读取zip文件而不是下载它(Zend)
我必须触发下载zip文件(Zip文件在我的数据文件夹中).
为此,我正在使用代码, $file = 'D:php7htdocsProjecttrunkapidatafile.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 在内容之间我可以看到zip中所有文件的名称.但它没有下载. 在我意识到这不起作用后,我开始搜索它并从堆栈流程中找到一些解决方案 尝试1:在每个随机行中添加不同的头元素和ob函数 > header(‘Content-Transfer-Encoding:binary’); 所有这些都是从不同的堆栈溢出问题和答案尝试并具有相同的结果 尝试2:PHP is reading file instead of downloading.这个问题没有任何公认的答案(他在询问核心php,但我只有zend的问题).我尝试了所有这些,但它没有用. 尝试3:Changing the .htaccess.之后我认为这是我的.htaccess的一个问题,并找到了更改.htaccess文件的答案. <FilesMatch ".(?i:zip)$"> ForceType application/octet-stream Header set Content-Disposition attachment </FilesMatch> 这也给了我相同的结果. 尝试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:php7htdocsProjecttrunkapidata' . $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"]); } }
基于此SO
answer,您可以尝试对您的功能进行以下修改.
public function getList(){ try{ //here i am getting the zip file name. $exportFile = $this->getRequest()->getQuery('exportid',''); $file = 'D:php7htdocsProjecttrunkapidata' . $exportFile . '.zip'; if (file_exists($file)) { $response = new ZendHttpResponseStream(); $response->setStream(fopen($file,'r')); $response->setStatusCode(200); $response->setStreamName(basename($file)); $headers = new ZendHttpHeaders(); $headers->addHeaders(array( 'Content-Description' => 'File Transfer','Content-Disposition' => 'attachment; filename="' . basename($file) .'"','Content-Type' => 'application/zip','Content-Length' => filesize($file) )); $response->setHeaders($headers); return $response; //return new JsonModel(["status"=>"Success"]); } else { return new JsonModel(["status"=>"Failed. No such file in "".$file."""]); } } catch(Exception $e){ return new JsonModel(["status"=>"Failed"]); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |