问题编号1:
您无法在空的zip档案中创建空的zip档案。 这将导致文件损坏。
问题编号2:
在您尚未关闭第一个zip存档的情况下,请勿尝试将其添加到另一个zip存档。
问题编号3:
$Bzip->addFile($file_path.$SzipN,$Szip); 那么,为什么要尝试将对象设置为文件名呢? => $Szip = new ZipArchive();
解:
//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/';
function zipFilesAndDownload($BzipN,$SzipN,$Bzip,$Szip,$file_path){
// Create the file Smallzip.zip and throw the error if unsuccessful
if ($Szip->open($SzipN, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open \n");
}
// Add a txt file to Smallzip so it isn't empty
$Szip->addFromString("testfilephp.txt", "#1 This is a test string added as testfilephp.txt.\n");
// Close Smallzip.zip as we're done with it
$Szip->close();
// Create the file Bigzip.zip and throw the error if unsuccessful
if ($Bzip->open($BzipN, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open \n");
}
// Add Smallzip.zip to Bigzip.zip with a valid name
$Bzip->addFile($file_path.$SzipN,$SzipN);
// Close Bigzip.zip as we're done with it
$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");
// Delete the files from the server, even if the user cancels the download
ignore_user_abort(true);
unlink($file_path.$SzipN);
unlink($file_path.$SzipN);
exit;
}
zipFilesAndDownload($BzipN,$SzipN,$Bzip,$Szip,$file_path);
?>