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"); //zip格式的
header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
header('Content-Length: '. filesize($file_dir)); //告诉浏览器,文件大小
@readfile($file_dir);
//销毁文件
unlink($file_dir);
exit();
压缩之后 不要立即下载 如果压缩内容过大 压缩未完成 下载之后的文件无法解压