我试图通过在本地服务器上创建zip文件来下载一些文件。该文件以zip格式下载,但当我试图提取它时,它给了我错误:
中心目录结束签名未找到。或者这个文件不是一个zip文件,或者它构成一个多部分存档的一个磁盘。在后一种情况下,将找到中央目录和zip文件注释
在此归档文件的最后一个磁盘。
这是我使用的代码:
function download_gallery($id) {
$this->load->library('zip');
$path = '/uploads/post/' . $id;
$this->zip->read_dir($path, FALSE);
$filename = $id . '.zip';
// http headers for zip downloads
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'); // mime type
header('Content-Disposition: attachment;filename="' . $filename . '"'); // tell browser what's the file name
header('Cache-Control: max-age=0'); // no cache
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($path, $filename));
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download($filename);
}
我检查了传递给函数的所有变量的值,都是正常的。
顺便说一下,我用的是codeigniter
我看到三个错误。
您从未调用$this->zip->archive($filename);
,因此zip文件在任何时候都不存在。
filesize($path, $filename)
是这个函数的错误用法。
filesize
只接受一个参数。我相信$this->zip->download($filename);
已经为下载设置了所需的头,没有必要自己这样做。