使用的TP框架,以及PHP自帶的壓縮類ZipArchive
展示效果
public function testzip()
{
$imgs = array(
"4" => array(
'https://ss3.baidu.com/9fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=b74adc2efddeb48fe469a7dec01e3aef/b812c8fcc3cec3fdb6b3e13ad888d43f869427eb.jpg',
'https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=8c46925c297f9e2f6f351b082f31e962/500fd9f9d72a605908e0c1a02634349b023bba93.jpg',
'https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=0fcb287ab4a1cd111ab674208913c8b0/b219ebc4b74543a97531368610178a82b9011456.jpg',
),
"7" => array(
'https://ss3.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=52ecd2493a9b033b3388fada25cf3620/77c6a7efce1b9d167b3c552cfddeb48f8d546480.jpg',
'https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=f978ff2b9aeef01f52141ec5d0ff99e0/c2fdfc039245d68899c8f4ebaac27d1ed31b24b2.jpg',
)
);
$name = "這里是中文";
$name = iconv('utf-8', 'GBK', $name);
//設置腳本的最大執(zhí)行時間,設置為0則無時間限制
set_time_limit(0);
ini_set('max_execution_time', '0');
$filename = ROOT_PATH . 'public/uploads/downzip/' . $name . '.zip';
$downname = $name . '.zip';
$zip = new \ZipArchive();
if ($zip->open($filename, \ZIPARCHIVE::CREATE) !== TRUE) {
exit('無法打開文件,或者文件創(chuàng)建失敗');
return 0;
}
foreach ($imgs as $key => $val) {
$i = 1;
foreach ($val as $k => $v) {
$zip->addFromString($key . '/' . $i . '.jpg', file_get_contents($v));
$i++;
}
}
$zip->close();
$fp = fopen($filename, "r");
$file_size = filesize($filename);//獲取文件的字節(jié)
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length:" . $file_size);
header("Content-Disposition: attachment; filename=$downname");
$buffer = 1024; //設置一次讀取的字節(jié)數(shù)悯仙,每讀取一次耗跛,就輸出數(shù)據(即返回給瀏覽器)
$file_count = 0; //讀取的總字節(jié)數(shù)
while (!feof($fp) && $file_count < $file_size) {
$file_con = fread($fp, $buffer);
$file_count += $buffer;
echo $file_con;
}
fclose($fp);
}