PHP压缩文件下载问题
我知道这个问题在这里被问了好几次,我在这里使用了很多建议,但没有一个对我有用 我的应用程序首先创建一个提要列表,然后用户选择要导出的提要。php文件为每个提要动态生成一个csv文件,并将它们添加到zip文件中。最后,我只是关闭zip文件并用PHP压缩文件下载问题,php,Php,我知道这个问题在这里被问了好几次,我在这里使用了很多建议,但没有一个对我有用 我的应用程序首先创建一个提要列表,然后用户选择要导出的提要。php文件为每个提要动态生成一个csv文件,并将它们添加到zip文件中。最后,我只是关闭zip文件并用readfile($zipfile)读取它 在firefox中使用FireBug进行调试时,响应头与预期一致,并且响应也是二进制格式的。但这不会在浏览器上生成“保存”对话框 现在,如果我获取要导出的URL并将其直接粘贴到新窗口上并单击,则会出现对话框 那么,这
readfile($zipfile)
读取它
在firefox中使用FireBug进行调试时,响应头与预期一致,并且响应也是二进制格式的。但这不会在浏览器上生成“保存”对话框
现在,如果我获取要导出的URL并将其直接粘贴到新窗口上并单击,则会出现对话框
那么,这里缺少什么,为什么现有页面无法生成对话框
这是我的密码
public function export_feeds($start, $end, $feedIds)
{
// Name of the file to export
$date = date('Y_m_j_H');
$fileName = 'Feeds_'.$date.'.zip';
// We create the ZIP file
$zip = new ZipArchive;
$result_zip = $zip->open($fileName, ZipArchive::CREATE); // We open the file
if ($result_zip === TRUE) {
//iterate through each row of the feedId and find the engine. Call the export function from the specifi engine class and collect the data
$feedIdsArry = explode(":",$feedIds);
foreach ($feedIdsArry as $value) {
$qresult = $this->mysqli->query("SELECT engine, name FROM feeds WHERE `id` = '$value'");
$row = $qresult->fetch_array();
if ($row['engine']==Engine::TIMESTORE) {
//$tableContent = $this->timestore->exportTable($value,$start,$end);
$tableContent = $this->mysqltimeseries->exportTable($value,$start,$end); //temporary workout
$tableContent = implode("\n", $tableContent);
}
if ($row['engine']==Engine::MYSQL) {
$tableContent = $this->mysqltimeseries->exportTable($value,$start,$end);
$tableContent = implode("\n", $tableContent);
}
$localFileName = $row['name'].'_'.$date.'.csv';
$zip->addFromString($localFileName, $tableContent);
}
$zip->close();
}
else {
echo 'Failed, code:' . $result_zip;
}
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-Disposition: attachment; filename=".$fileName);
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($fileName));
if(!file_exists($fileName)) {
die('Zip file not found');
}
else {
readfile($fileName); // To download the ZIP file
}
exit;
}
公共函数导出\u提要($start、$end、$feedId)
{
//要导出的文件的名称
$date=日期('Y_m_j_H');
$fileName='Feeds.'$date..zip';
//我们创建ZIP文件
$zip=新的ZipArchive;
$result\u zip=$zip->open($fileName,ZipArchive::CREATE);//我们打开文件
如果($result\u zip==TRUE){
//遍历feedId的每一行并找到引擎。从指定的引擎类调用导出函数并收集数据
$feedIdsArry=explode(“:”,$feedIds);
foreach($feedIdsArry作为$value){
$qresult=$this->mysqli->query(“从feed中选择引擎、名称,其中'id`='$value'”);
$row=$qresult->fetch_array();
if($row['engine']==engine::TIMESTORE){
//$tableContent=$this->timestore->exportTable($value、$start、$end);
$tableContent=$this->mysqltimeseries->exportTable($value,$start,$end);//临时训练
$tableContent=内爆(“\n”,$tableContent);
}
if($row['engine']==engine::MYSQL){
$tableContent=$this->mysqltimeseries->exportTable($value、$start、$end);
$tableContent=内爆(“\n”,$tableContent);
}
$localFileName=$row['name'.''.$date..csv';
$zip->addFromString($localFileName,$tableContent);
}
$zip->close();
}
否则{
echo“失败,代码:”。$result\u-zip;
}
标题(“Pragma:public”);
标题(“到期日:0”);
标头(“缓存控制:必须重新验证,后检查=0,前检查=0”);
标头(“缓存控制:公共”);
标题(“内容描述:文件传输”);
标题(“内容类型:应用程序/zip”);
标题(“内容处置:附件;文件名=”.$filename);
标题(“内容传输编码:二进制”);
标题(“内容长度:”.filesize($fileName));
如果(!file_存在($fileName)){
die('Zip文件未找到');
}
否则{
readfile($fileName);//下载ZIP文件
}
出口
}
以下是Firebug的响应标题
Cache-Control public
Connection Keep-Alive
Content-Description File Transfer
Content-Disposition attachment; filename=Feeds_2013_10_24_14.zip
Content-Length 1600
Content-Type application/force-download
Date Thu, 24 Oct 2013 01:39:38 GMT
Expires 0
Keep-Alive timeout=5, max=100
Pragma public
Server Apache/2.4.6 (Win32) PHP/5.5.4
X-Powered-By PHP/5.5.4
content-transfer-encoding binary
缓存控制公用
连接保持活动状态
内容描述文件传输
内容处置附件;filename=Feeds\u 2013\u 10\u 24\u 14.zip
内容长度1600
内容类型应用程序/强制下载
日期2013年10月24日星期四01:39:38 GMT
过期0
保持活动超时=5,最大值=100
布拉格公共
服务器Apache/2.4.6(Win32)PHP/5.5.4
X-Powered-By-PHP/5.5.4
内容传输编码二进制
尝试使用以下标题: 原始响应
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename="'.$filename.'"');
更新
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename="'.$filename.'"');
您也可以尝试使用cURL而不是readfile(),类似于:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
然后:
if(!file_exists($fileName)) {
die('Zip file not found');
}
else {
$file_content = file_get_contents($fileName); // To download the ZIP file
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename="'.$filename.'"');
echo $file_content;
}
exit;
你是如何链接到下载的?没有到zip文件下载的链接,当点击导出按钮时,这是我调用的代码。我用Firebug的响应标题编辑了我的帖子。我还编辑了我的回复,也许用cURL代替readfile()会有所帮助。我不是100%确定,但你可以试试。它仍然是一样的,我将尝试通过发送csv文件来查看它是否与zip文件相关。csv不起作用,在所有浏览器中都检查了这一问题。我使用了你的原始代码,以及我的答案中包含的代码,这两种代码都起作用。唯一的区别是我的PHP版本(我怀疑这是问题的原因)和我的平台:服务器:Apache/2.4.6(Unix)PHP/5.5.1。我想知道你的导出按钮是如何调用这个函数的?但这确实很奇怪,从理论上讲,你所拥有的一切都应该“起作用”。