unzip_file( string $file, string $to ): true|WP_Error
- Since
- 2.5.0
- Source
wp-admin/includes/file.php:1614
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.
Compatibility
- WordPress
- since 2.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$filestring- Full path and filename of ZIP archive.
$tostring- Full path on the filesystem to extract archive to.
Return value
true|WP_Error- True on success, WP_Error on failure.
Performance profile
How much work a call to unzip_file() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 12–75
- Plugin surface
- 1 hook
- Called by
- 1
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 88.
Third-party callbacks on 'unzip_file_use_ziparchive' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - regexregular expression over the whole input
preg_split()called directly
Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 19 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost unzip_file() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 12–14 | __() |
is_object($wp_filesystem) && ->is_dir() | 26 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), _unzip_file_pclzip() |
is_object($wp_filesystem) && ->is_dir() && !apply_filters() | 31 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), apply_filters(), _unzip_file_pclzip() |
is_object($wp_filesystem) && ->is_dir() && apply_filters() && $result === true | 34 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), apply_filters(), _unzip_file_ziparchive() |
is_object($wp_filesystem) && !->is_dir() && !$i | 38 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), untrailingslashit(), preg_split(), _unzip_file_pclzip() |
is_object($wp_filesystem) && ->is_dir() && apply_filters() && $result !== true && is_wp_error() | 42 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), apply_filters(), _unzip_file_ziparchive(), is_wp_error(), ->get_error_code() |
is_object($wp_filesystem) && ->is_dir() && apply_filters() && $result !== true && !is_wp_error() | 43 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), apply_filters(), _unzip_file_ziparchive(), is_wp_error(), _unzip_file_pclzip() |
is_object($wp_filesystem) && !->is_dir() && !$i && !apply_filters() | 43 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), untrailingslashit(), preg_split(), apply_filters(), _unzip_file_pclzip() |
is_object($wp_filesystem) && !->is_dir() && !$i && apply_filters() && $result === true | 46 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), untrailingslashit(), preg_split(), apply_filters(), _unzip_file_ziparchive() |
is_object($wp_filesystem) && ->is_dir() && apply_filters() && $result !== true && is_wp_error() | 47 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), apply_filters(), _unzip_file_ziparchive(), is_wp_error(), ->get_error_code(), _unzip_file_pclzip() |
is_object($wp_filesystem) && $i && !empty($path[$i]) && !$dir | 54 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), untrailingslashit(), preg_split(), array_slice(), ->is_dir(), _unzip_file_pclzip() |
is_object($wp_filesystem) && !->is_dir() && !$i && apply_filters() && $result !== true && is_wp_error() | 54 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), untrailingslashit(), preg_split(), apply_filters(), _unzip_file_ziparchive(), is_wp_error(), ->get_error_code() |
7 further outcomes, up to 75 instructions
is_object($wp_filesystem) && !->is_dir() && !$i && apply_filters() && $result !== true && !is_wp_error() | 55 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), untrailingslashit(), preg_split(), apply_filters(), _unzip_file_ziparchive(), is_wp_error(), _unzip_file_pclzip() |
is_object($wp_filesystem) && $i && !empty($path[$i]) && !$dir && !apply_filters() | 59 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), untrailingslashit(), preg_split(), array_slice(), ->is_dir(), apply_filters(), _unzip_file_pclzip() |
is_object($wp_filesystem) && !->is_dir() && !$i && apply_filters() && $result !== true && is_wp_error() | 59 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), untrailingslashit(), preg_split(), apply_filters(), _unzip_file_ziparchive(), is_wp_error(), ->get_error_code(), _unzip_file_pclzip() |
is_object($wp_filesystem) && $i && !empty($path[$i]) && !$dir && apply_filters() && $result === true | 62 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), untrailingslashit(), preg_split(), array_slice(), ->is_dir(), apply_filters(), _unzip_file_ziparchive() |
is_object($wp_filesystem) && $i && !empty($path[$i]) && !$dir && apply_filters() && $result !== true && is_wp_error() | 70 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), untrailingslashit(), preg_split(), array_slice(), ->is_dir(), apply_filters(), _unzip_file_ziparchive(), is_wp_error(), ->get_error_code() |
is_object($wp_filesystem) && $i && !empty($path[$i]) && !$dir && apply_filters() && $result !== true && !is_wp_error() | 71 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), untrailingslashit(), preg_split(), array_slice(), ->is_dir(), apply_filters(), _unzip_file_ziparchive(), is_wp_error(), _unzip_file_pclzip() |
is_object($wp_filesystem) && $i && !empty($path[$i]) && !$dir && apply_filters() && $result !== true && is_wp_error() | 75 | wp_raise_memory_limit(), trailingslashit(), ->is_dir(), untrailingslashit(), preg_split(), array_slice(), ->is_dir(), apply_filters(), _unzip_file_ziparchive(), is_wp_error(), ->get_error_code(), _unzip_file_pclzip() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 88 | 12–75 | 12 | |
| 8.5 | 88 | 12–75 | 12 | |
| 8.4 | 88 | 12–75 | 12 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 97 | 12–84 | 12 | |
| 8.2 | 97 | 12–84 | 12 | |
| 8.1 | 97 | 12–84 | 12 | |
| 7.4 | 97 | 12–84 | 12 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while unzip_file() runs, in this order:
- apply_filters( unzip_file_use_ziparchive )filterline 1655 (+41 into the body)
Filters whether to use ZipArchive to unzip archives.
Uses · 9
- __()Retrieves the translation of $text.
- wp_raise_memory_limit()Attempts to raise the PHP memory limit for memory intensive processes.
- trailingslashit()Appends a trailing slash.
- untrailingslashit()Removes trailing forward slashes and backslashes if they exist.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- _unzip_file_ziparchive()Attempts to unzip an archive using the ZipArchive class.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- _unzip_file_pclzip()Attempts to unzip an archive using the PclZip library.
- WP_Error::__construct()Initializes the error.
Used by · 1
- WP_Upgrader::unpack_package()Unpacks a compressed package file.
Source code
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 );}Changelog
Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-admin/includes/file.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.