实现二维码生成打包并下载

      发布在:个人笔记      评论:0 条评论
public function export_ewm()
{

set_time_limit(0);
$search = $this->request->post('search');
$ids = $this->request->post('ids');
$filter = $this->request->post('filter');
$op = $this->request->post('op');
$columns = $this->request->post('columns');

$whereIds = $ids == 'all' ? '1=1' : ['deputy.id' => ['in', explode(',', $ids)]];
$this->request->get(['search' => $search, 'ids' => $ids, 'filter' => $filter, 'op' => $op]);
$this->relationSearch=true;
list($where, $sort, $order, $offset, $limit) = $this->buildparams();


$protocol=$this->request->isSsl()?'https://' : 'http://';
$url = $protocol . $_SERVER['HTTP_HOST'].'/index/index/res/unicode/';



$list=$this->model
->where($where)->with(['county','street','station'])
->where($whereIds)
->select();
if(!$list){
$this->error(__('No rows were found'));
}

/* 2. 指定中文目录名(可按需改成 POST 参数) */
$cnFolder = date('YmdHis').rand(1000,9999); // 想叫啥写啥
$tmpDir = ROOT_PATH . '/public/tmp/' . $cnFolder; // 完整路径
if (!is_dir($tmpDir) && !mkdir($tmpDir, 0777, true)) {
exit('创建临时目录失败,请检查权限或路径');
}
foreach ($list as $k=>$v){
$qrCode = new QrCode($this->request->domain().'/index/appeal/index?npc_deputy_id='.$v["id"]);
$qrCode->setSize(300)->setMargin(0);
$writer = new PngWriter();

$png = $writer->write($qrCode)->getString();
$mulu=$tmpDir . '/' .$v['county']['name'].'-'.$v['street']['name'].'-'.$v['station']['name'];
if (!is_dir($mulu) && !mkdir($mulu, 0777, true)) {
exit('创建目录失败:' . $mulu);
}
$safeName = preg_replace('/[\/\\\\:\*\?"<>|]/u', '_', $v['name']);
file_put_contents($mulu . '/' . $v['id'] . '.' . $safeName . '.png', $png);
}

/* 4. 打包(把中文目录整体打进去,解压后仍能看到中文目录) */
$zipFile = ROOT_PATH . '/public/tmp/' . $cnFolder. '.zip';
$zip = new \ZipArchive();
if ($zip->open($zipFile, \ZipArchive::CREATE) !== true) {
exit('无法创建 zip');
}
/* 4.1 递归添加目录 */
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($tmpDir, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST
);

foreach ($iterator as $file) {
$localPath = str_replace($tmpDir . DIRECTORY_SEPARATOR, '', $file->getPathname());
// GBK,让 Windows 自带解压不乱码
$gbkPath = iconv('UTF-8', 'GBK//IGNORE', $cnFolder . '/' . $localPath);
if ($file->isDir()) {
$zip->addEmptyDir($gbkPath);
} else {
$zip->addFile($file->getPathname(), $gbkPath);
}
}

$zip->close();
$this->redirect('/tmp/' . $cnFolder. '.zip');
}
相关文章
热门推荐