【发布时间】:2015-09-11 23:06:49
【问题描述】:
大家好,这几天我一直在尝试解决一个问题而没有任何回应,我不擅长 php,但我正在尽我所能。
我想自动下载,而不是在服务器上保存一个名为 Bigzip 的 zip
在这个 Bigzip 中:-另一个名为 Smallzip 的 zip
但打开下载的 zip 时出现错误,它已损坏
<?php
//Big and Small Archives names
$BzipN='Bigzip.zip';
$SzipN='Smallzip.zip';
//Big and Small Archives
$Bzip = new ZipArchive();
$Szip = new ZipArchive();
//File path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/PF/';
zipFilesAndDownload($BzipN,$SzipN,$Bzip,$Szip,$file_path);
function zipFilesAndDownload($BzipN,$SzipN,$Bzip,$Szip,$file_path)
{
//create the file and throw the error if unsuccessful
if ($Bzip->open($BzipN, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$BzipN>\n");
}
if ($Szip->open($SzipN, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$SzipN>\n");
}
//Add Smallzip to BigZip
$Bzip->addFile($file_path.$SzipN,$Szip);
$Szip->close();
$Bzip->close();
//then send the headers to foce download the Big zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$BzipN");
header("Content-length: " . filesize($BzipN));
header("Pragma: no-cache");
header("Expires: 0");
readfile("$BzipN");
exit;
}
?>
如果您有任何替代方案、想法、建议,我将很乐意接受。
谢谢
【问题讨论】:
-
确保 htaccess 没有干扰:
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|zip)$ no-gzip dont-vary -
@ScottMcGready 他的代码简直是一团糟。