# 打包下载文件
public function Download(){
$id = explode(",", $_GET['id']);
$type = $_GET['type'];
$conditionss['mpid'] = get_mpid();
$conditionss['id']=array('in',$id);
$data = M('business_emails')->order("visa_time desc")->where($conditionss)->field('username,evisa,receipt')->select();
foreach ($data as $key => $value) {
if ($type == 1) {
$visa[] = $value['evisa']; # 电子签
$username[] = $value['username']; # 姓名
} else if ($type == 2) {
$invoice[] = $value['receipt']; # 电子发票
$username[] = $value['username']; # 姓名
} else{
$visa[] = $value['evisa']; # 电子签
$invoice[] = $value['receipt']; # 电子发票
$username[] = $value['username']; # 姓名
}
}
if(!empty($visa) || !empty($invoice)){
$zip = new \ZipArchive();
//用于前端跳转zip链接拼接
$path_redirect = '/zip/'.date('Ymd');
//临时文件存储地址
$path = '/tmp'.$path_redirect;
if(!is_dir($path)){
mkdir($path, 0777,true);
}
$filename = time().'download.zip';
$zipname = $path.'/'.$filename;
$zip->open($zipname, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
foreach ($invoice as $key => $url) {
$fileContent = $this->CurlDownload($url);#curl获取链接内容
if( $fileContent){
$__tmp = $this->SaveFile( $url , $path , $fileContent );# 保存临时文件
$names = $username[$key].'.pdf';# 文件名
$zip->addFromString( iconv('utf-8', 'gbk//ignore', 'invoice/'.$names), file_get_contents($__tmp[0]));//中文使用这个
// if ($__tmp[1]) {
// $names = end(explode("/", $url));# 文件名
// $end = end(explode("/", $__tmp[0]));
// }else{
// $names = end(explode("/", $url)).'.pdf';# 文件名
// $end = end(explode("/", $__tmp[0])).'.pdf';
// }
// $zip->addFile($__tmp[0], 'invoice/'.$end); # 英文数字没问题 中文乱码
// $zip->renameName('invoice/'.$end, 'invoice/'.$names);
}
}
foreach ($visa as $key => $url) {
$fileContent = $this->CurlDownload($url);#curl获取链接内容
if( $fileContent){
$__tmp = $this->SaveFile( $url , $path , $fileContent );# 保存临时文件
$names = $username[$key].'.pdf';# 文件名
$zip->addFromString( iconv('utf-8', 'gbk//ignore', 'visa/'.$names), file_get_contents($__tmp[0]));//中文使用这个
// if ($__tmp[1]) {
// $names = end(explode("/", $url));# 文件名
// $end = end(explode("/", $__tmp[0]));
// }else{
// $names = end(explode("/", $url)).'.pdf';# 文件名
// $end = end(explode("/", $__tmp[0])).'.pdf';
// }
// $zip->addFile($__tmp[0], 'visa/'.$end);
// $zip->renameName('visa/'.$end, 'visa/'.$names);
}
}
$zip->close();# 关闭连接
//直接写文件的方式下载到客户端
if (file_exists($zipname)) {
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename=附件.zip'); //文件名
header("Content-Type: application/zip"); //zip格式的
header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
header('Content-Length: ' . filesize($zipname)); //告诉浏览器,文件大小
@readfile($zipname);
}
//删除临时文件
@unlink($zipname);
}
}
# curl获取链接内容
public function CurlDownload($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$errno = curl_errno($ch);
$error = curl_error($ch);
$res=curl_exec($ch);
curl_close($ch);
if($errno>0){
return '';
}
return $res;
}
# 保存临时文件
public function SaveFile( $url ,$dir , $content){
$fname = basename($url); //返回路径中的文件名部分
$str_name = pathinfo($fname); //以数组的形式返回文件路径的信息
$extname = strtolower($str_name['extension']); //把扩展名转换成小写
$path = $dir.'/'.md5($url).$extname;
$fp = fopen( $path ,'w+' );
fwrite( $fp , $content );
fclose($fp);
return array( $path , $extname) ;
}