Tuesday, October 7, 2025
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
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6708 POSTS0 COMMENTS
Nicole Veronica
11872 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6829 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6780 POSTS0 COMMENTS
Umr Jansen
6784 POSTS0 COMMENTS