php - 如何在 Laravel 6 中提取 zip 文件

标签 php laravel zip

我正在运行最新的 Laravel 6 并想上传和提取 ZIP 文件。我尝试使用 Composer chupper/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();
                }

zip 已成功上传到正确的位置。

$target_path 的例子是 /var/www/html/project/storage/app/1/courses/1/random.zip
但是,我收到一个错误:

SplFileInfo::getSize(): stat failed for /tmp/php59z5uT



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

当我点击 $zip->open 时会弹出功能。

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

最佳答案

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

使用: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
}

我希望这可以帮到你

关于php - 如何在 Laravel 6 中提取 zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57951450/

相关文章:

php - 如何从 UITableViewCell 获取 SQL 信息并用作 MYSQL 解析的凭据

php - 阻止人们通过表单上传恶意 PHP 文件

Python - 仅压缩包含数据的列表

php - TinyMCE 和 Fancybox - 编辑器无法在第二个 View 上工作

php - 尝试从数据库打印结果时出现未定义的错误

PHP mkdir 权限由于只读属性而被拒绝在 Windows Server 2008 IIS 7 上运行?

laravel - Laradock 端口范围长度不匹配错误

PHP Trait 覆盖 protected Trait 方法

Laravel:什么是模型?

arrays - 将目录读取为 []byte 而不压缩它