当前位置:Gxlcms > PHP教程 > 求PHP打包下载图片的代码

求PHP打包下载图片的代码

时间:2021-07-01 10:21:17 帮助过:3人阅读

如题,
注意:不是做图片采集,图片已在服务器上,需求是用户勾选了几张图片后,下载到用户的电脑上,单个没问题,多个不知道如何弄

回复内容:

如题,
注意:不是做图片采集,图片已在服务器上,需求是用户勾选了几张图片后,下载到用户的电脑上,单个没问题,多个不知道如何弄

$zip = new \ZipArchive;
//压缩文件名
$filename = 'download.zip';
//新建zip压缩包
$zip->open($filename,\ZipArchive::OVERWRITE);
//把图片一张一张加进去压缩
foreach ($images as $key => $value) {
    $zip->addFile($value);
}
//打包zip
$zip->close();

//可以直接重定向下载
header('Location:'.$filename);

//或者
输出下载 header("Cache-Control: public"); header("Content-Description: File Transfer"); header('Content-disposition: attachment; filename='.basename($filename)); //文件名 header("Content-Type: application/force-download"); header("Content-Transfer-Encoding: binary"); header('Content-Length: '. filesize($filename)); //告诉浏览器,文件大小 readfile($filename);

人气教程排行