PHP ZipArchive extractTo() Function
Last Updated :
27 Mar, 2023
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
$zip = new ZipArchive;
if ( $zip ->open( 'Geeks.zip' )) {
if ( $zip ->extractTo( 'GFG_Folder' )) {
echo 'File Extracted Successfully' ;
}
else {
echo 'Fail to Extract File' ;
}
$zip ->close();
}
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
$zip = new ZipArchive;
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' ;
}
$zip ->close();
}
else {
echo 'Failed to open zip file' ;
}
?>
|
Output:
Reference: https://www.php.net/manual/en/ziparchive.extractto.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...