/**
* @title 批量下载图片
* @description 批量下载图片
* @author 卡卡
* @method POST
*/
public function download_batch()
{
$img_space_ids = input('img_space_ids');
$result = getSelect('img_space',['img_space_id'=>['in',$img_space_ids]],'create_time desc','file_path');
$array = array();
foreach ($result as $k=>$v){
array_push($array,$this->url.$v['file_path']);
}
$tmpFile = tempnam(sys_get_temp_dir(), ''); //临时文件
$zip = new \ZipArchive(); //php内置的压缩类
$zip->open($tmpFile, \ZipArchive::CREATE);
foreach ($array as $value) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $value);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$fileContent = curl_exec($ch);
curl_close($ch);
$zip->addFromString(basename($value), $fileContent); //将文件循环压缩到压缩包
}
$zip->close();
header('Content-Type: application/zip');
header("Content-disposition: attachment; filename=图片下载.zip");
header("'Content-Length: ' . filesize($tmpFile)");
readfile($tmpFile);
unlink($tmpFile);
}
/**
* @title 单独下载图片
* @description 单独下载图片
* @author 卡卡
* @method POST
*/
public function download()
{
$img_space_id = input('img_space_id');
$img_space_info = getFind('img_space',['img_space_id'=>$img_space_id],'img_space_id,file_path,file_name');
$localfile = ROOT_PATH . 'public' . DS . $img_space_info['file_path'];
$file_file_size = filesize ($localfile);
$file_path = $localfile;
# 以只读和二进制模式打开文件
$file = fopen ( $file_path, "rb" );
# 告诉浏览器这是一个文件流格式的文件
Header ( "Content-type: application/octet-stream" );
# 请求范围的度量单位
Header ( "Accept-Ranges: bytes" );
# Content-Length是指定包含于请求或响应中数据的字节长度
Header ( "Accept-Length: " . $file_file_size );
# 用来告诉浏览器,文件是可以当做附件被下载,下载后的文件名称为$file_name该变量的值。
Header ( "Content-Disposition: attachment; filename=" . $img_space_info['file_name'] );
# 读取文件内容并直接输出到浏览器
echo fread ( $file, $file_file_size );
fclose ( $file );
exit ();
}