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

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS