我知道这个问题在这里已经被问了好几次了,我在这里使用了很多的建议,但是没有一个对我有用。PHP Zip文件下载问题
我的应用程序首先创建一个Feed列表,然后用户选择要导出的Feed。 php文件为每个提要动态生成一个csv文件,并将它们添加到zip文件中。最后,我只是关闭zip文件并用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; }
这里是萤火虫
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
你是如何链接到下载? – Chausser
当点击导出按钮时,没有链接到zip文件下载,这是我打电话的代码。 – Shankar