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

我必须触发下载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"]);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值