unzip_file()
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.
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.
Hooks from the function
Return
true|WP_Error
. True on success, WP_Error on failure.
Usage
unzip_file( $file, $to );
- $file(string) (required)
- Full path and filename of ZIP archive.
- $to(string) (required)
- Full path on the filesystem to extract archive to.
Examples
#1 Basic example
Showing how to unzip the contents of a .zip file, that resides in the WordPress upload directory, to the same destination.
WP_Filesystem(); $destination = wp_upload_dir(); $destination_path = $destination['path']; $unzipfile = unzip_file( "{$destination_path}/filename.zip", $destination_path ); if ( $unzipfile ) { echo 'Successfully unzipped the file!'; } else { echo 'There was an error unzipping the file.'; }
#2 Example of unzipping an archive with unzip_file()
Although this function requires initialization of the file system API, it is not a method of the $wp_filesystem object, so it must be passed parameters with $wp_filesystem in mind.
The first parameter, $file, must be the absolute path to the file (on the server), and the parameter $to must point to the absolute path of the WordPress file system.
// even in the admin panel, check if the function is defined if ( ! function_exists( 'unzip_file' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } // and the global variable must contain an object global $wp_filesystem; if ( ! $wp_filesystem ) { // but if it does not, this function corrects the situation WP_Filesystem(); }
define( 'MY_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); // this variable must already have been set when initializing the file system global $wp_filesystem; // remote path to the file system $plugin_path = str_replace( ABSPATH, $wp_filesystem->abspath(), MY_PLUGIN_DIR ); // the RIGHT way to use the function $file = MY_PLUGIN_DIR . '/plugin-file.zip'; $to = $plugin_path; $result = unzip_file( $file, $to ); if( $result === true ){ // OK: the archive is unpacked } // the wrong way to use the function // $to cannot be a direct path to the folder, otherwise the FTP and SSH methods remain inoperable $file = MY_PLUGIN_DIR . '/plugin-file.zip'; $to = MY_PLUGIN_DIR; unzip_file( $file, $to ); // the wrong way to use the function // If $file is not a "direct" absolute path, // then users who do not use FTP and SSH methods are left out $file = $plugin_path . '/plugin-file.zip'; $to = $plugin_path; unzip_file( $file, $to );
Notes
- Global. WP_Filesystem_Base. $wp_filesystem WordPress filesystem subclass.
Changelog
Since 2.5.0 | Introduced. |