Download.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\controller\api;;
  3. use crmeb\basic\BaseController;
  4. use think\facade\Db;
  5. use crmeb\services\WechatTemplateService;
  6. class Download extends BaseController
  7. {
  8. /**
  9. * 下载图片并生成压缩包
  10. * @param $data 图片数组,一维
  11. * @param $tmpDir 存放路径
  12. * @return string
  13. */
  14. public function download()
  15. {
  16. $data=Db::name('qrcode_zip')->where('is_status',0)->select()->toArray();
  17. $view='';
  18. foreach ($data as $k=>$v){
  19. $view .="<li><a href='".$v['url']."'>".date("Y-m-d H:i:s",$v['ctime'])."</a></li> <br>";
  20. }
  21. return $view;
  22. $limit = (int)$this->request->params(['limit'])['limit']??100;
  23. if($limit>100)$limit=100;
  24. $picAllArr=Db::name('merchant_qrcode')->where('uid',0)->where('is_status',0)->where('mer_id',0)->limit($limit)->select();
  25. // $picAllArr=Db::name('merchant_qrcode')->where('id','>','3198')->column('show_src')->select();
  26. if($picAllArr){
  27. $picAllArr = $picAllArr -> toArray();
  28. }else{
  29. return app('json')->fail('暂无未绑定的二维码');
  30. }
  31. $tmpDir = './public/zip/'; //
  32. if (!file_exists($tmpDir)) {
  33. //创建文件夹
  34. mkdir($tmpDir, 0777, true);
  35. }
  36. $zipName = date('His') . mt_rand(1000, 9999) . '.zip'; // 压缩包文件名
  37. $zipNameUrl = $tmpDir . $zipName; // 文件路径
  38. // 生成文件
  39. $zip = new \ZipArchive();
  40. if ($zip->open($zipNameUrl, \ZipArchive::OVERWRITE) !== true) {
  41. //OVERWRITE 参数会覆写压缩包的文件 文件必须已经存在
  42. if ($zip->open($zipNameUrl, \ZipArchive::CREATE) !== true) {
  43. // 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
  44. return app('json')->fail('下载失败,文件夹不存在');
  45. }
  46. }
  47. foreach ($picAllArr as $file) {
  48. Db::name('merchant_qrcode')->where('id',$file['id'])->update(['is_status'=>1]);
  49. //判断图片是否存在
  50. $isFile = $this->checkFileExists($file['show_src']);
  51. if (!$isFile) {
  52. continue;
  53. }
  54. //抓取图片内容
  55. $fileContent = file_get_contents($file['show_src']);
  56. //添加图片
  57. $zip->addFromString(basename($file['show_src']), $fileContent);
  58. }
  59. // 关闭
  60. $zip->close();
  61. //没有文件
  62. if (!file_exists($zipNameUrl)) {
  63. return app('json')->fail('下载失败,图片不存在或无法下载');
  64. }
  65. ob_start();
  66. header("Cache-Control: public");
  67. header("Content-Description: File Transfer");
  68. header('Content-disposition: attachment; filename=' . $zipNameUrl); //文件名
  69. header("Content-Type: application/zip"); //zip格式的
  70. header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
  71. header('Content-Length: ' . filesize($zipNameUrl)); //告诉浏览器,文件大小
  72. // 下面2步必须要
  73. ob_clean();
  74. flush();
  75. @readfile($zipNameUrl);
  76. // unlink($zipNameUrl); // 删除文件
  77. }
  78. /**
  79. * 判断文件是否存在,支持本地及远程文件
  80. * @param String $file 文件路径
  81. * @return Boolean
  82. */
  83. private function checkFileExists($file)
  84. {
  85. // 远程文件
  86. if (strtolower(substr($file, 0, 5)) == 'https') {
  87. // 远程文件
  88. $header = get_headers($file, true);
  89. return isset($header[0]) && (strpos($header[0], '200') || strpos($header[0], '304'));
  90. } elseif (strtolower(substr($file, 0, 4)) == 'http') {
  91. // 远程文件
  92. $header = get_headers($file, true);
  93. return isset($header[0]) && (strpos($header[0], '200') || strpos($header[0], '304'));
  94. } else {
  95. // 本地文件
  96. return file_exists($file);
  97. }
  98. }
  99. }