| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace app\controller\api;;
- use crmeb\basic\BaseController;
- use think\facade\Db;
- use crmeb\services\WechatTemplateService;
- class Download extends BaseController
- {
- /**
- * 下载图片并生成压缩包
- * @param $data 图片数组,一维
- * @param $tmpDir 存放路径
- * @return string
- */
- public function download()
- {
- $data=Db::name('qrcode_zip')->where('is_status',0)->select()->toArray();
- $view='';
- foreach ($data as $k=>$v){
- $view .="<li><a href='".$v['url']."'>".date("Y-m-d H:i:s",$v['ctime'])."</a></li> <br>";
- }
- return $view;
- $limit = (int)$this->request->params(['limit'])['limit']??100;
- if($limit>100)$limit=100;
- $picAllArr=Db::name('merchant_qrcode')->where('uid',0)->where('is_status',0)->where('mer_id',0)->limit($limit)->select();
- // $picAllArr=Db::name('merchant_qrcode')->where('id','>','3198')->column('show_src')->select();
- if($picAllArr){
- $picAllArr = $picAllArr -> toArray();
- }else{
- return app('json')->fail('暂无未绑定的二维码');
- }
- $tmpDir = './public/zip/'; //
- 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 app('json')->fail('下载失败,文件夹不存在');
- }
- }
- foreach ($picAllArr as $file) {
- Db::name('merchant_qrcode')->where('id',$file['id'])->update(['is_status'=>1]);
- //判断图片是否存在
- $isFile = $this->checkFileExists($file['show_src']);
- if (!$isFile) {
- continue;
- }
- //抓取图片内容
- $fileContent = file_get_contents($file['show_src']);
- //添加图片
- $zip->addFromString(basename($file['show_src']), $fileContent);
- }
- // 关闭
- $zip->close();
- //没有文件
- if (!file_exists($zipNameUrl)) {
- return app('json')->fail('下载失败,图片不存在或无法下载');
- }
- ob_start();
- header("Cache-Control: public");
- header("Content-Description: File Transfer");
- header('Content-disposition: attachment; filename=' . $zipNameUrl); //文件名
- header("Content-Type: application/zip"); //zip格式的
- header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
- header('Content-Length: ' . filesize($zipNameUrl)); //告诉浏览器,文件大小
- // 下面2步必须要
- ob_clean();
- flush();
- @readfile($zipNameUrl);
- // unlink($zipNameUrl); // 删除文件
- }
- /**
- * 判断文件是否存在,支持本地及远程文件
- * @param String $file 文件路径
- * @return Boolean
- */
- private function checkFileExists($file)
- {
- // 远程文件
- if (strtolower(substr($file, 0, 5)) == 'https') {
- // 远程文件
- $header = get_headers($file, true);
- return isset($header[0]) && (strpos($header[0], '200') || strpos($header[0], '304'));
- } elseif (strtolower(substr($file, 0, 4)) == 'http') {
- // 远程文件
- $header = get_headers($file, true);
- return isset($header[0]) && (strpos($header[0], '200') || strpos($header[0], '304'));
- } else {
- // 本地文件
- return file_exists($file);
- }
- }
- }
|