.zip 文件下载为 .php [英] .zip file downloading as .php

查看:46
本文介绍了.zip 文件下载为 .php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 php 在服务器上生成的 .zip 文件.生成的文件是有效的,我通过 ftp 等下载它进行了检查.

I have a .zip file which is generated on the server using php. The file generated is valid and I checked it by downloading it via ftp etc.

我需要创建一种方法,让用户在生成此文件后下载该文件,然后删除该文件.这是我要发送的标题.

I need to create a way for a user to download this file after it is generated and then the file is deleted. Here are the headers that I am sending.

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-control: public");
header("Content-Description: File Transfer");
header('Content-type: application/zip'); 
header("Content-Transfer-Encoding: Binary");
header('Content-Disposition: attachment; filename="'.basename($archive_file_name).'"');
header("Content-Length: " . filesize($this->dirPath."/".$archive_file_name) );
ob_clean();
//echo is_file($this->dirPath."/".$archive_file_name);
readfile($this->dirPath."/".$archive_file_name);
unlink($this->dirPath."/".$archive_file_name);
exit;

当我尝试下载前几次时,上面的代码有效,但几轮后它开始下载为 .php 文件而不是 .zip

The code above works when I try to download the first few times but after few turns it starts downloading as .php file instead of .zip

文件下载是通过转到开始创建 zip 文件的特定链接来触发的.完成后,它会发送标头以开始下载

The file download is triggered by going to a specific link which starts the creation of zip file. Once it is done, it sends out the headers to begin the download

推荐答案

您的代码看起来正确.但是,您需要确保代码前后有空格.
在您的代码中添加标题时,空间会中断并且不会加载 zip 文件.
再次检查并删除页面顶部和底部的空格.

your code looks proper. However you need to ensure the preceding and succeeding spaces for your code.
while adding headers in your code, the space will disrupt and will not load the zip file.
Check once again and remove the spaces on top and bottom of page.

还有一行写着
readfile($this->dirPath."/".$archive_file_name);
可以从代码中删除

And a line which says
readfile($this->dirPath."/".$archive_file_name);
can be removed from code

这篇关于.zip 文件下载为 .php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆