public function qrcode_export() { $ids = input('ids'); if (!$ids) { $this->error('数据异常'); } $ids = explode(',', $ids); $word = Db::name('word'); $word_data = $word->where('word_id', 'in', $ids)->select(); $fileList = []; foreach ($word_data as $index => $word_datum) { $path = ROOT_PATH . 'Public/' . $word_datum['code_path']; if ($word_datum['code_path'] && file_exists($path)) { $img = $word_datum['code_path']; } else { $rr = request()->domain(); $rr = $rr . url('student/home/code', ['wordId' => $word_datum['word_id']]); $name = $word_datum['grade_id'] . '_' . $word_datum['word_name']; $img = $this->qrcode($rr, $name); $word->where('word_id', $word_datum['word_id'])->update(['code_path' => $img]); } array_push($fileList, $img); } $this->zip_download($fileList, 'code'); }
public function zip_download($path, $filename) { // 最终生成的文件名(含路径) $filename = ROOT_PATH . 'public/qrcode/' . $filename . '.zip'; // 如果存在压缩文件,删除 if (file_exists($filename)) { unlink($filename); } //重新生成文件 $zip = new ZipArchive(); if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) { exit('无法打开文件,或者文件创建失败'); return 0; } // 要压缩的文件 $datalist = $path; // 遍历添加待压缩文件 foreach ($datalist as $val) { $p = ROOT_PATH . 'Public/' . $val; if (file_exists($p)) { //val:qrcode/1_about.png $zip->addFile($val); } } // 关闭 $zip->close(); if (!file_exists($filename)) { // 即使创建,仍有可能失败 exit('无法找到文件'); return 0; } header('Content-type: application/zip'); header('Content-Disposition: attachment; filename="qrcode_download.zip"'); readfile($filename); return 1; }