PHP ZipArchive拓展实现多文件打包下载

首先需要开启ZipArchive拓展(php.ini)

在这里插入图片描述

确认拓展已开启,没有找到的可以下载放到php下的ext目录

在这里插入图片描述

链接:https://pan.baidu.com/s/16BNkqa7IV1BfyUbYXa1wXw
提取码:jmmn

phpinfo()确认拓展开启后,即可使用

在这里插入图片描述

PHP代码如下:

// 批量下载二维码
    public function uploadEwm(){
		// 小数组(前端表格多选传过来)
		$codes = "P202105260929478396,P202105260817563449,P202105250850237432,P202105170956254203,P202105161028448863";
        // 根据逗号分割,将字符串转成数组
        $code_array = explode(',', $codes);

        // 查询数量
        $count = count($code_array);
        if ($count > 0) {
            // 先批量生成二维码(这块做的功能是如果没有二维码就先批量生成,然后再打包进行下载)
            $this->createQrCode($code_array, $type);

            // 循环拼接数据
            foreach ($code_array as $key => $value) {
                $files[] = './vendor/phpqrcode/img/'.$value.'.png';
            }

            // 引用PHP->ZipArchive类
            $zip = new \ZipArchive;
            // 定义压缩包文件名
            $zipName = 'code.zip';
            // 没有文件则创建
            if ($zip->open($zipName, \ZIPARCHIVE::OVERWRITE | \ZIPARCHIVE::CREATE)!==TRUE) {
                exit('无法打开文件,或者文件创建失败');
            }
            // 循环给zip压缩包写入文件
            foreach($files as $val){
                if(file_exists($val)){
                    /* 
                        addFile函数首个参数如果带有路径,则压缩的文件里包含的是带有路径的文件压缩
                        若不希望带有路径,则需要该函数的第二个参数
                    */
                    $zip->addFile($val, basename($val)); // 第二个参数是放在压缩包中的文件名称,如果文件可能会有重复,就需要注意一下
                }else{
                    exit('网络原因,无法下载二维码文件');
                }
            }

            // 关闭
            $zip->close(); 
            // 下面是输出下载;
            header("Cache-Control: max-age=0");
            header("Content-Description: File Transfer");
            header('Content-disposition: attachment; filename=' .date('Y-m-d').$zipName); // 文件名
            header("Content-Type: application/zip"); // zip格式的
            header("Content-Transfer-Encoding: binary"); // 告诉浏览器,这是二进制文件
            header('Content-Length: ' . filesize($zipName)); // 告诉浏览器,文件大小
            @readfile($zipName);//输出文件;
            // 删除服务器文件资源
            unlink($zipName);
            exit;
        } else {
            $this->error('请搜索需要下载的数据');
        }
    }

	// 二维码加汉字
    public function getQRcode($url, $code, $content){
        // 调用phpqrcode
        Vendor('phpqrcode.phpqrcode');
        $qrcode = new \QRcode();
        // 拼接二维码图片地址
        $shareimg = 'fail'.$code . '.png';
        $shareimg =  './vendor/phpqrcode/img/'. $shareimg;
        // 首先生成一张不带文字的二维码图片
        /*
            参数1:二维码里的内容
            参数2:生成二维码的保存地址
            参数3:二维码可被覆盖的百分比,可以控制二维码的容错率(L->7%, M->15%, Q->25%, H->30%)
            参数4:生成二维码图片的大小
            参数5:控制生成二维码图片的空白区域大小
        */
        $qrcode->png($code, $shareimg, 'M', 12, 13);
        // 先读取存好的二维码图片,再追加文字
        $image = imagecreatefrompng($shareimg);
        // 字体文件
        $font = './vendor/phpqrcode/font/simsun.ttc'; 
        // 文字颜色
        $color = imagecolorallocate($image,0,0,0); 
        // 创建二维码图片下文字
        foreach ($content as $key => $value) {
            if($key==0){
                /*
					参数1:读取现有的二维码图片
					参数2:字体大小
					参数3:字体倾斜的角度
					参数4,5:文字的x,y轴坐标
					参数6:文字的颜色
					参数7:字体的样式(引入的字体文件)
					参数8:二维码下方的文字内容
				*/
                imagettftext($image, 15, 0, 150, 430, $color, $font, $value); // 创建文字(编码)
            }else{
                // 这里每行加24,设置的是文字的行距
                imagettftext($image, 15, 0, 150, $key*24+430, $color, $font, $value); // 创建文字(循环内容)
            }  
        }

        // 保存新生成的二维码图片
        imagepng($image, $url);
        // 删除不带文字的二维码图片
        unlink($shareimg);
    }

传送门

PHP生成二维码并添加文字(phpqrcode类)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
我安装laravel的一个扩展时,提示没有ZipArchive 我根据国外的一个网站的提示进行操作,任然不成功,后重新修改了这个包才成功 首先要执行下面操作 brew update brew install php@7.3 brew link php@7.3 然后执行 Step 1: Install PEAR/PECL support cd /tmp curl -s -O https://pear.php.net/install-pear-nozlib.phar sudo php install-pear-nozlib.phar -d /usr/local/lib/php -b /usr/local/bin (credit) Step 2: Download zip extension source I tried installing zip with pecl, but that failed because the PHP header files were missing. So I downloaded the source for the zip extension from pecl. Step 3:下载我提供的包文件 然后解压 Step 4: Copy/modify phpsize and php-config In order to make phpize and php-config find the PHP header files in the location that XCode places them rather than in /usr/include/php -- a read-only location now -- I followed some tips I found elsewhere and copied and modified phpize and php-config to change the include directory in each of them. cp /usr/bin/phpize /usr/local/bin/phpize cp /usr/bin/php-config /usr/local/bin/php-config Step 5: Build the zip PHP extension With all of those changes in place, I could now build the zip extension. cd zip-1.15.5 phpize ./configure -with-php-config=/usr/local/bin/php-config make Step 6: Install zip PHP extension make install fails to install the extension, again because of the read-only file system. So instead I created an extension directory under /usr/local/php. mkdir -p /usr/local/php/extensions cp modules/zip.so /usr/local/php/extensions/zip.so Step 7: Update your PHP.ini Finally we need to tell PHP to load this extension. sudo vim /etc/php.ini Add the following line: extension=/usr/local/php/extensions/zip.so Restart apache with sudo apachectl restart and you'll see the zip extension being loaded now.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值