WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/unzip file

Description

Unzips a specified ZIP file to a location on the Filesystem via the WordPress Filesystem Abstraction. Assumes that WP_Filesystem() has already been called and set up. Does not extract a root-level __MACOSX directory, if present.

Attempts to increase the PHP Memory limit to 256M before uncompressing. However, the most memory required shouldn't be much larger than the Archive itself.

Usage

<?php unzip_file$file$to ); ?>

Example

Basic example showing how to unzip the contents of a .zip file, that resides in the WordPress upload directory, to the same destination.

<?php
WP_Filesystem
();
$destination wp_upload_dir();
$destination_path $destination['path'];
$unzipfile unzip_file$destination_path.'/filename.zip'$destination_path);
   
   if ( 
is_wp_error$unzipfile ) ) {
         echo 
'There was an error unzipping the file.'
   } else {
      echo 
'Successfully unzipped the file!';       
   }
?>

Parameters

$file
(string) (required) Full path and filename of zip archive
Default: None
$to
(string) (required) Full path on the filesystem to extract archive to
Default: None

Return Values

(mixed) 
WP_Error on failure, True on success

Change Log

Since: 2.5

Source File

unzip_file() is located in wp-admin/includes/file.php