【问题标题】:How to extract zip file in Laravel 6如何在 Laravel 6 中提取 zip 文件
【发布时间】:2019-09-16 06:31:03
【问题描述】:

我正在运行最新的 Laravel 6 并且想要上传和解压 ZIP 文件。我尝试使用 composer chumper/zipper - 但它似乎与 Laravel 6 不兼容。所以我正在尝试使用 PHP zipArchive。

我正在尝试的过程的代码是:

                    $docfileext1 =  $request->file('czipfile')->getClientOriginalExtension();
                $random_filename1 = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz123"), -5);
                $newfilename1 = $random_filename1.'.'.$docfileext1;
                //First check this is a ZIP file
                if ($docfileext1 <> 'zip') {
                    return back(); //->witherrors('this is not a ZIP file. Please select a zip file');
                }

                $request->file('czipfile')->move(
                    base_path() . '/storage/app/'.$oid.'/courses/'.$inscourse, $newfilename1
                );

                //Now unzip
                $target_path = base_path() . '/storage/app/' .$oid. '/courses/'. $inscourse.'/'.$newfilename1;              
                $extract_path = base_path() . '/storage/app/' .$oid. '/courses/'. $inscourse.'/';


                $zip = new ZipArchive();
                $x = $zip->open($target_path);
                if ($x === true) {
                    $zip->extractTo($extract_path);
                    $zip->close();
                }

压缩包已成功上传到正确的位置。

$target_path 的例子是/var/www/html/project/storage/app/1/courses/1/random.zip

但是,我收到一个错误:

SplFileInfo::getSize(): /tmp/php59z5uT 统计失败

我检查了 php.ini 文件,其大小和内存远高于我尝试使用的 700kb 测试 zip 文件。

当我点击$zip-&gt;open 函数时弹出。

关于我在这里做错了什么的任何想法,或者有更好的方法吗?衷心感谢。

【问题讨论】:

    标签: php laravel zip


    【解决方案1】:

    我也在找这个,我找到了这个方法,我不是专家,但这可以工作

    使用:vipsoft/unzip

    我的代码是这样的

    if(isset($request->zipfile)){
       $unzipper  = new Unzip();
       $file = $request->zipfile->store('public'); //store file in storage/app/zip
       $filenames = $unzipper->extract(storage_path('app/'.$file),storage_path('app/public'));
       //extract the files in storage/app/public 
       dd($filenames); //show file names
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 2016-07-17
      • 1970-01-01
      相关资源
      最近更新 更多