9951 explained code solutions for 126 technologies


php-laravelHow can I use PHP Laravel's ZipArchive library to create a zip file?


To use the ZipArchive library to create a zip file in PHP Laravel, you can take the following steps:

  1. Include the ZipArchive library with the following code:
use ZipArchive;
  1. Create a new ZipArchive object:
$zip = new ZipArchive;
  1. Create a new zip file:
$zip->open('my_zip_file.zip', ZipArchive::CREATE);
  1. Add files to the zip file:
$zip->addFile('file1.txt');
$zip->addFile('file2.txt');
  1. Close the zip file:
$zip->close();
  1. Verify the zip file was created:
if ($zip->open('my_zip_file.zip') === TRUE) {
    echo 'Zip file created successfully!';
    $zip->close();
}
  1. Output:
Zip file created successfully!

Helpful links

Edit this code on GitHub