how to unzip in php

In PHP, unzipping a zip file can be done using the ZipArchive class which provides methods to create, open, and extract files from a zip archive.

Here's an example code snippet that demonstrates how to unzip files using the ZipArchive class:

main.php
$zip = new ZipArchive();
$zipFileName = "example.zip"; // Name of the zip file to extract files from
$destination = "extracted_files/"; // Destination folder for extracted files

if ($zip->open($zipFileName) === true) {
  $zip->extractTo($destination);
  $zip->close();
  echo "Files extracted successfully!";
} else {
  echo "Error: Unable to open zip file";
}
362 chars
12 lines

In the above example, we first create a new instance of the ZipArchive class. We then specify the name of the zip file to extract files from and the destination folder where the extracted files should be stored.

Next, we open the zip file using the open() method of the ZipArchive class. If the file is successfully opened, we extract the files to the specified destination using the extractTo() method. Finally, we close the zip file using the close() method and display a success message to the user.

Note: Please make sure that the destination folder has write permissions, otherwise the extraction will fail.

gistlibby LogSnag