Reputation: 252
I am running the latest Laravel 6 and wanting to upload and extract a ZIP file. I tried using the composer chumper/zipper - but seems it not compatible with Laravel 6 yet. So I am trying to use PHP zipArchive.
My code for the proccess I am trying is:
$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();
}
The zip IS succesfully uploading to the correct location.
example of $target_path is /var/www/html/project/storage/app/1/courses/1/random.zip
However, I get an error:
SplFileInfo::getSize(): stat failed for /tmp/php59z5uT
I have checked the php.ini file and the sizes and memory are way higher than the 700kb test zip file I am trying to use.
Which is popping up when I hit the $zip->open
function.
Any ideas on what I am doing wrong here OR is there a better way? Sincerely appreciated.
Upvotes: 4
Views: 17348
Reputation: 370
I was looking for this too and I found this method, I'm not an expert but this does the job
using : vipsoft/unzip
my code was like this
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
}
I hope this was helpful
Upvotes: 7