Tuesday, June 16, 2026
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
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS