laravel 文件处理 压缩/解压zip

1:将此软件包添加到所需软件包列表中composer.json

"chumper/zipper": "1.0.x"

2:命令行执行

composer update

3:配置app/config/app.php

add to providers Chumper\Zipper\ZipperServiceProvider::class
add to aliases 'Zipper' => Chumper\Zipper\Zipper::class

4:遍历文件打包至压缩包

$files = Array();
        foreach ($student as $key => $data) {
            if ($data->photopath != null) {
                $check = glob(storage_path('photo/' . $data->photopath));
                $files = array_merge($files, $check);
            }
        }
 Zipper::make(storage_path() . '/systemImg/' . $name)->add($files)->close();

5:读取压缩包文件

   Zipper::make( storage_path() . '/photo/photos')->extractTo(storage_path('temp'));
  $zip = new \ZipArchive();//方法2:流处理,新建一个ZipArchive的对象
                $logFiles = Zipper::make($path)->listFiles('/\.png$/i');
                if ($zip->open($path) === TRUE) {
                    foreach ($logFiles as $key) {
                        $stream = $zip->getStream($key);
                        $str = stream_get_contents($stream); //这里注意获取到的文本编码
                        $name = iconv("utf-8", "gb2312//IGNORE", $key);
                        file_put_contents(storage_path() . '\temp\\' . $name, $str);
                    }
                } else {
                    return '{"statusCode":"300", "message":"上传失败,请检查照片"}';
                }

猜你喜欢

转载自blog.csdn.net/qq_29099209/article/details/80180883