PHP生成压缩包
$zip = new \ZipArchive;
$file = time().'.zip';
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ?"https://": "http://";
$siteurl = $protocol . $_SERVER['HTTP_HOST'].'/';
if ($zip->open($file, \ZipArchive::CREATE) === TRUE)
{
foreach ($array as $key => $value){
$fieltop = $value['room_num'];
$re = $zip->addFile($url, $fieltop.'/room/'.$key .'.png');
}
$zip->close();
}
rename($file,ROOT_PATH."public/uploads/zip/".$file);
文档链接
PHP直接把压缩文件输出到浏览器
用以下方法 下载 解决 下载出现1k 压缩包无法使用的情况
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($url));
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($file_dir));
@readfile($file_dir);
unlink($file_dir);
exit();
压缩之后 不要立即下载 如果压缩内容过大 压缩未完成 下载之后的文件无法解压
|