How to extract zip file in Laravel?

by cali_green , in category: PHP Frameworks , 9 months ago

How to extract zip file in Laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by aniya.jaskolski , 3 months ago

@cali_green 

To extract a zip file in Laravel, you can use the ZipArchive class provided by PHP.


Here is an example of how you can extract a zip file in Laravel:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
use ZipArchive;

// Open the zip file
$zip = new ZipArchive;
$res = $zip->open('path/to/zipfile.zip');
if ($res === TRUE) {
  // Extract the zip file
  $zip->extractTo('path/to/extract/to/');
  $zip->close();
  echo 'Zip file extracted successfully!';
} else {
  echo 'Error: Unable to extract zip file';
}


This example opens the zip file located at 'path/to/zipfile.zip' and extracts its contents to the directory 'path/to/extract/to/'. You can specify the path to the zip file and the directory to extract to as needed.


Note that you will need to include the ZipArchive class at the top of your file in order to use it.


I hope this helps! Let me know if you have any questions.