加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 13:04:41 所属栏目:PHP教程 来源:网络整理
导读:我必须触发下载zip文件(Zip文件在我的数据文件夹中). 为此,我正在使用代码, $file = 'D:php7htdocsProjecttrunkapidatafile.zip';header('Content-Description: File Transfer');header('Content-type: application/zip');header('Content-dispositio
我必须触发下载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
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文件的答案.

<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"]);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读