On this page
unzip_file( string $file, string $to ): true|WP_Error
Unzips a specified ZIP file to a location on the filesystem via the WordPress Filesystem Abstraction.
Description
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.
Parameters
$filestring Required-
Full path and filename of ZIP archive.
$tostring Required-
Full path on the filesystem to extract archive to.
Return
Source
File: wp-admin/includes/file.php. View all references
function unzip_file( $file, $to ) {
global $wp_filesystem;
if ( ! $wp_filesystem || ! is_object( $wp_filesystem ) ) {
return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
}
// Unzip can use a lot of memory, but not this much hopefully.
wp_raise_memory_limit( 'admin' );
$needed_dirs = array();
$to = trailingslashit( $to );
// Determine any parent directories needed (of the upgrade directory).
if ( ! $wp_filesystem->is_dir( $to ) ) { // Only do parents if no children exist.
$path = preg_split( '![/\\\]!', untrailingslashit( $to ) );
for ( $i = count( $path ); $i >= 0; $i-- ) {
if ( empty( $path[ $i ] ) ) {
continue;
}
$dir = implode( '/', array_slice( $path, 0, $i + 1 ) );
if ( preg_match( '!^[a-z]:$!i', $dir ) ) { // Skip it if it looks like a Windows Drive letter.
continue;
}
if ( ! $wp_filesystem->is_dir( $dir ) ) {
$needed_dirs[] = $dir;
} else {
break; // A folder exists, therefore we don't need to check the levels below this.
}
}
}
/**
* Filters whether to use ZipArchive to unzip archives.
*
* @since 3.0.0
*
* @param bool $ziparchive Whether to use ZipArchive. Default true.
*/
if ( class_exists( 'ZipArchive', false ) && apply_filters( 'unzip_file_use_ziparchive', true ) ) {
$result = _unzip_file_ziparchive( $file, $to, $needed_dirs );
if ( true === $result ) {
return $result;
} elseif ( is_wp_error( $result ) ) {
if ( 'incompatible_archive' !== $result->get_error_code() ) {
return $result;
}
}
}
// Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
return _unzip_file_pclzip( $file, $to, $needed_dirs );
}
Hooks
- apply_filters( 'unzip_file_use_ziparchive',
bool $ziparchive ) -
Filters whether to use ZipArchive to unzip archives.
Related
Uses
| Uses | Description |
|---|---|
| wp_raise_memory_limit() wp-includes/functions.php | Attempts to raise the PHP memory limit for memory intensive processes. |
| _unzip_file_ziparchive() wp-admin/includes/file.php | Attempts to unzip an archive using the ZipArchive class. |
| _unzip_file_pclzip() wp-admin/includes/file.php | Attempts to unzip an archive using the PclZip library. |
| untrailingslashit() wp-includes/formatting.php | Removes trailing forward slashes and backslashes if they exist. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| trailingslashit() wp-includes/formatting.php | Appends a trailing slash. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
| WP_Error::__construct() wp-includes/class-wp-error.php | Initializes the error. |
Used By
| Used By | Description |
|---|---|
| WP_Upgrader::unpack_package() wp-admin/includes/class-wp-upgrader.php | Unpack a compressed package file. |
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/unzip_file