public function export($ids)
{
p
i
c
A
l
l
A
r
r
=
a
r
r
a
y
c
o
l
u
m
n
(
d
b
:
:
t
a
b
l
e
(
′
f
a
p
h
o
t
o
f
i
l
e
′
)
−
>
w
h
e
r
e
I
n
(
′
i
d
′
,
picAllArr = array_column(db::table('fa_photo_file')->whereIn('id',
picAllArr=arraycolumn(db::table(′faphotofile′)−>whereIn(′id′,ids)->field(‘fullurl’)->select(), ‘fullurl’);
$tmpDir = ‘/www/wwwroot/demo.dzqiji.com/public/uploads’; // 类似于/www/public/upload/
if (!file_exists($tmpDir)) {
//创建文件夹
mkdir($tmpDir, 0777, true);
}
$zipName = date('His') . mt_rand(1000, 9999) . '.zip'; // 压缩包文件名
$zipNameUrl = $tmpDir . $zipName; // 文件路径
// 生成文件
$zip = new \ZipArchive();
if ($zip->open($zipNameUrl, \ZipArchive::OVERWRITE) !== true) {
//OVERWRITE 参数会覆写压缩包的文件 文件必须已经存在
if ($zip->open($zipNameUrl, \ZipArchive::CREATE) !== true) {
// 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
return '下载失败,文件夹不存在';
}
}
foreach ($picAllArr as $file) {
//判断图片是否存在
$isFile = $this->checkFileExists($file);
if (!$isFile) {
continue;
}
//抓取图片内容
$fileContent = file_get_contents($file);
//添加图片
$zip->addFromString(basename($file), $fileContent);
}
// 关闭
$zip->close();
//没有文件
if (!file_exists($zipNameUrl)) {
return '下载失败,图片不存在或无法下载';
}
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename=' . $zipName); //文件名
header("Content-Type: application/zip"); //zip格式的
header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
header('Content-Length: ' . filesize($zipNameUrl)); //告诉浏览器,文件大小
// 下面2步必须要
ob_clean();
flush();
@readfile($zipNameUrl);
unlink($zipNameUrl); // 删除文件
exit;
}