标签:zip header 二维码 打包 file sourceTemp qrcode PHP dir
1. composer安装qrcode
composer require 2amigos/qrcode-library
2. 生成二维码
use Da\QrCode\QrCode; public function getCode($code) { $dir = iconv("UTF-8", "GBK", './qrcode'); mkdir($dir, 0777, true); $url = Yii::$app->request->hostInfo . '/area?code=' . $code; //二维码内容自定义 $qrCode = (new QrCode($url)) ->setSize(250) ->setMargin(5) // ->setForegroundColor(51, 153, 255) // ->setBackgroundColor(51, 153, 255) ->writeFile(Yii::$app->basePath . '/web/qrcode/' . $code . '.png'); }
3.打包文件夹并下载
<?php namespace app\libs; class downLoadZip { public function downLoadFiles() { $exportPath = './qrcode'; $filename = $exportPath . '.zip'; $zip = new \ZipArchive(); //参数1:zip保存路径,参数2:ZIPARCHIVE::CREATE没有即是创建 if (!$zip->open($filename, \ZIPARCHIVE::CREATE)) { echo "创建[exportPath.zip]失败<br/>"; return; } //echo "创建[$exportPath.zip]成功<br/>"; $this->createZip(opendir($exportPath), $zip, $exportPath); $zip->close(); header("Cache-Control: public"); header("Content-Description: File Transfer"); header('Content-disposition: attachment; filename=' . basename($filename)); //文件名 header("Content-Type: application/zip"); //zip格式的 header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件 header('Content-Length: ' . filesize($filename)); //告诉浏览器,文件大小 $this->deldir('./qrcode'); @readfile($filename); unlink('qrcode.zip'); //删除服务器压缩文件,释放服务器资源 } function createZip($openFile, $zipObj, $sourceAbso, $newRelat = '') { while (($file = readdir($openFile)) != false) { if ($file == "." || $file == "..") continue; /*源目录路径(绝对路径)*/ $sourceTemp = $sourceAbso . '/' . $file; /*目标目录路径(相对路径)*/ $newTemp = $newRelat == '' ? $file : $newRelat . '/' . $file; if (is_dir($sourceTemp)) { //echo '创建'.$newTemp.'文件夹<br/>'; $zipObj->addEmptyDir($newTemp);/*这里注意:php只需传递一个文件夹名称路径即可*/ $this->createZip(opendir($sourceTemp), $zipObj, $sourceTemp, $newTemp); } if (is_file($sourceTemp)) { //echo '创建'.$newTemp.'文件<br/>'; $zipObj->addFile($sourceTemp, $newTemp); } } } public function deldir($dir) { //先删除目录下的文件: $dh = opendir($dir); while ($file = readdir($dh)) { if ($file != "." && $file != "..") { $fullpath = $dir . "/" . $file; if (!is_dir($fullpath)) { unlink($fullpath); } else { $this->deldir($fullpath); } } } } }
标签:zip,header,二维码,打包,file,sourceTemp,qrcode,PHP,dir 来源: https://www.cnblogs.com/han-bit/p/15215467.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。