Open In App

PHP ZipArchive extractTo() Function

Last Updated : 26 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The ZipArchive::extractTo() function is an inbuilt function in PHP that is used to extract the zip archive content in a folder.

Syntax:

bool ZipArchive::extractTo(
string $pathto,
array|string|null $files = null
)

Parameters: This function accepts two parameters that are described below:

  • $pathto: This parameter holds the location where you want to extract the archive files.
  • $files: This parameter holds the files that you want to extract.

Return Value: This function returns “true” on success and “false” on failure.

Example 1: The following code demonstrates the extractTo() function which creates a new folder.

PHP
<?php

  // Create a new ZipArchive object
  $zip = new ZipArchive;

  // Check for opening the zip file
  if ($zip->open('Geeks.zip')) {
      if($zip->extractTo('GFG_Folder')) {
          echo 'File Extracted Successfully';
      }
      else {
          echo 'Fail to Extract File';
      } 

      // Close the zip file
      $zip->close();
    }
    // If zip file is not open/exist
    else {
        echo 'Failed to open zip file';
    }
?>

Output:

Example 2: The following code demonstrates the extractTo() function which creates a new folder with the path “GFG_Folder/Geeks”.

PHP
<?php

    // Create a new ZipArchive object
    $zip = new ZipArchive;

    // Check for opening the zip file
    if ($zip->open('Geeks.zip', ZipArchive::CREATE)) {

        $ext = $zip->extractTo('GFG_Folder/Geeks');

        if($ext) {
            echo 'File Extracted Successfully';
        }
        else {
            echo 'Fail to Extract File';
        } 

        // Close the zip file
        $zip->close();
    }

    // If zip file is not open/exist
    else {
        echo 'Failed to open zip file';
    }
?>

Output:

Reference: https://www.php.net/manual/en/ziparchive.extractto.php



Next Article

Similar Reads