How to Extract Zip file using PHP

Learn how to extract a zip file using the PHP ZipArchive class. In the previous post, we have explained how to create ZIP file in PHP.

ZipArchive is a simple PHP utility class that provides us the functionality to create a zip file or to extract the zip file dynamically on the server.

It has a extractTo() method, which extracts the zip file contents to the specified destination. The ZipArchive class also has a lot of other methods and properties to help you get more information about the archive before extracting all its contents.

Syntax:

bool ZipArchive::extractTo(string $destination, mixed $entries);

Parameters:

destination: This parameter can be used to specify the zip file.
entries: This parameter can be used to specify a single file name that is to be extracted.

Example: Extract Zip File in PHP

Create a ExtractorZip Class

The Extractor class helps to extract zip files on the server using PHP ZipArchive.

ExtractorZip.class.php

Create index.php to Extract ZIP in PHP

Use ExtractorZip class to extract zip file to the specified path using PHP.

  • Include and initialize the ExtractorZip class.
  • Mention the directory path of the zip file, that you want to extract.
  • Mention the destination to extract the ZIP file on the server.
  • Call the extractZipMethod() function of ExtractorZip class.

index.php

Recommended Posts For You

About Harish

I am professional web developer and blogger. Use this blog to share own api and other web development experience with you. I do accept paid work. Write to me at - [email protected]

View all posts by Harish