Home  >  Article  >  Backend Development  >  求PHP打包下载图片的代码

求PHP打包下载图片的代码

WBOY
WBOYOriginal
2016-06-06 20:07:52984browse

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

回复内容:

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

<code class="php">$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);</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn