我正在开发一个项目,我必须创建zip文件并通过电子邮件发送给最终用户,我尝试了以下代码
function createZipAndDownload($files, $filesPath, $zipFileName)
{
// Create instance of ZipArchive. and open the zip folder.
$zip = new \ZipArchive();
if ($zip->open($zipFileName, \ZipArchive::CREATE) !== TRUE) {
exit("cannot open <$zipFileName>\n");
}
// Adding every attachments files into the ZIP.
foreach ($files as $file) {
$zip->addFile($filesPath . $file, $file);
}
$zip->close();
// Download the created zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename = $zipFileName");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$zipFileName");
exit;
}
// Files which need to be added into zip
$files = array('abc.pdf','abc.xlsx');
// Directory of files
$filesPath = $_SERVER['DOCUMENT_ROOT'] . '/zipper/zipper/files/';
// Name of creating zip file
$zipName = 'document.zip';
echo createZipAndDownload($files, $filesPath, $zipName);
它运行正常并且zip文件被下载但是当我尝试提取它时显示错误并且文件将不被提取。谢谢