Wednesday, November 20, 2024
Google search engine
HomeLanguagesPHP ZipArchive extractTo() Function

PHP ZipArchive extractTo() Function

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

RELATED ARTICLES

Most Popular

Recent Comments