_unzip_file_ziparchive( string $file, string $to, string[] $needed_dirs = array() ): true|WP_Error
- Since
- 3.0.0
- Source
wp-admin/includes/file.php:1677
Description
This function should not be called directly, use unzip_file() instead.
Assumes that WP_Filesystem() has already been called and set up.
Compatibility
- WordPress
- since 3.0.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.
$needed_dirsstring[]optional- A partial list of required folders needed to be created.Default:
array()
Return value
true|WP_Error- True on success, WP_Error on failure.
Performance profile
How much work a call to _unzip_file_ziparchive() 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
- 25–115
- Plugin surface
- 2 hooks
- 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 281.
Third-party callbacks on 'pre_unzip_file', 'unzip_file' 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
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 · 15 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _unzip_file_ziparchive() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$zopen is null, false, long, double, string, array, object, resource | 25 | ->open(), __(), ziparchive_error() |
$zopen is not null, false, long, double, string, array, object, resource && $i | 36 | ->open(), ->statIndex(), ->close(), __() |
$zopen is not null, false, long, double, string, array, object, resource && !$i && !wp_doing_cron() && $pre !== null | 51–53 | ->open(), wp_doing_cron(), array_unique(), asort(), apply_filters(), ->close() |
$zopen is not null, false, long, double, string, array, object, resource && !$i && wp_doing_cron() && $available_space | 52 | ->open(), wp_doing_cron(), disk_free_space(), ->close(), __(), compact() |
$zopen is not null, false, long, double, string, array, object, resource && !$i && !wp_doing_cron() && !$needed_dirs && !->mkdir() && !->is_dir() | 59–60 | ->open(), wp_doing_cron(), array_unique(), asort(), ->mkdir(), ->is_dir(), ->close(), __() |
$zopen is not null, false, long, double, string, array, object, resource && !$i && wp_doing_cron() && $pre !== null | 60–64 | ->open(), wp_doing_cron(), disk_free_space(), array_unique(), asort(), apply_filters(), ->close() |
$zopen is not null, false, long, double, string, array, object, resource && !$i && !wp_doing_cron() && $pre === null | 66–68 | ->open(), wp_doing_cron(), array_unique(), asort(), apply_filters(), ->close(), apply_filters() |
$zopen is not null, false, long, double, string, array, object, resource && !wp_doing_cron() && $pre === null | 68–70 | ->open(), wp_doing_cron(), array_unique(), asort(), apply_filters(), ->statIndex(), ->close(), __() |
$zopen is not null, false, long, double, string, array, object, resource && !$i && wp_doing_cron() && !$needed_dirs && !->mkdir() && !->is_dir() | 68–71 | ->open(), wp_doing_cron(), disk_free_space(), array_unique(), asort(), ->mkdir(), ->is_dir(), ->close(), __() |
$zopen is not null, false, long, double, string, array, object, resource && !$i && wp_doing_cron() && $pre === null | 75–79 | ->open(), wp_doing_cron(), disk_free_space(), array_unique(), asort(), apply_filters(), ->close(), apply_filters() |
$zopen is not null, false, long, double, string, array, object, resource && wp_doing_cron() && $pre === null | 77–81 | ->open(), wp_doing_cron(), disk_free_space(), array_unique(), asort(), apply_filters(), ->statIndex(), ->close(), __() |
$zopen is not null, false, long, double, string, array, object, resource && !wp_doing_cron() && $pre === null && !str_ends_with() && $contents === false | 93–95 | ->open(), wp_doing_cron(), array_unique(), asort(), apply_filters(), ->statIndex(), str_ends_with(), validate_file(), ->getFromIndex(), ->close(), __() |
3 further outcomes, up to 115 instructions
$zopen is not null, false, long, double, string, array, object, resource && !wp_doing_cron() && $pre === null && !str_ends_with() && $contents !== false && !->put_contents() | 102–104 | ->open(), wp_doing_cron(), array_unique(), asort(), apply_filters(), ->statIndex(), str_ends_with(), validate_file(), ->getFromIndex(), ->put_contents(), ->close(), __() |
$zopen is not null, false, long, double, string, array, object, resource && wp_doing_cron() && $pre === null && !str_ends_with() && $contents === false | 102–106 | ->open(), wp_doing_cron(), disk_free_space(), array_unique(), asort(), apply_filters(), ->statIndex(), str_ends_with(), validate_file(), ->getFromIndex(), ->close(), __() |
$zopen is not null, false, long, double, string, array, object, resource && wp_doing_cron() && $pre === null && !str_ends_with() && $contents !== false && !->put_contents() | 111–115 | ->open(), wp_doing_cron(), disk_free_space(), array_unique(), asort(), apply_filters(), ->statIndex(), str_ends_with(), validate_file(), ->getFromIndex(), ->put_contents(), ->close(), __() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 280 | 25–114 | 29 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 281 | 25–115 | 29 | |
| 8.4 | 281 | 25–115 | 29 | 18 fewer instructions than PHP 8.3 |
| 8.3 | 299 | 25–118 | 29 | |
| 8.2 | 299 | 25–118 | 29 | 1 more instruction than PHP 8.1 |
| 8.1 | 298 | 25–146 | 29 | 4 fewer instructions than PHP 7.4 |
| 7.4 | 302 | 25–148 | 29 |
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 · 2
2 hooks fire while _unzip_file_ziparchive() runs, in this order:
- apply_filters( pre_unzip_file )filterline 1786 (+109 into the body)
Filters archive unzipping to override with a custom process.
- apply_filters( unzip_file )filterline 1842 (+165 into the body)
Filters the result of unzipping an archive.
Uses · 10
- __()Retrieves the translation of $text.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- validate_file()Validates a file name and path against an allowed set of rules.
- str_ends_with()Polyfill for `str_ends_with()` function added in PHP 8.0.
- untrailingslashit()Removes trailing forward slashes and backslashes if they exist.
- wp_doing_cron()Determines whether the current request is a WordPress cron request.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- ZipArchive::__construct()
- WP_Error::__construct()Initializes the error.
Used by · 1
- unzip_file()Unzips a specified ZIP file to a location on the filesystem via the WordPress Filesystem Abstraction.
Source code
function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) { global $wp_filesystem; $z = new ZipArchive(); $zopen = $z->open( $file, ZIPARCHIVE::CHECKCONS ); if ( true !== $zopen ) { return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), array( 'ziparchive_error' => $zopen ) ); } $uncompressed_size = 0; for ( $i = 0; $i < $z->numFiles; $i++ ) { $info = $z->statIndex( $i ); if ( ! $info ) { $z->close(); return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) ); } if ( str_starts_with( $info['name'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory. continue; } // Don't extract invalid files: if ( 0 !== validate_file( $info['name'] ) ) { continue; } $uncompressed_size += $info['size']; $dirname = dirname( $info['name'] ); if ( str_ends_with( $info['name'], '/' ) ) { // Directory. $needed_dirs[] = $to . untrailingslashit( $info['name'] ); } elseif ( '.' !== $dirname ) { // Path to a file. $needed_dirs[] = $to . untrailingslashit( $dirname ); } } // Enough space to unzip the file and copy its contents, with a 10% buffer. $required_space = $uncompressed_size * 2.1; /* * disk_free_space() could return false. Assume that any falsey value is an error. * A disk that has zero free bytes has bigger problems. * Require we have enough space to unzip the file and copy its contents, with a 10% buffer. */ if ( wp_doing_cron() ) { $available_space = function_exists( 'disk_free_space' ) ? @disk_free_space( WP_CONTENT_DIR ) : false; if ( $available_space && ( $required_space > $available_space ) ) { $z->close(); return new WP_Error( 'disk_full_unzip_file', __( 'Could not copy files. You may have run out of disk space.' ), compact( 'uncompressed_size', 'available_space' ) ); } } $needed_dirs = array_unique( $needed_dirs ); foreach ( $needed_dirs as $dir ) { // Check the parent folders of the folders all exist within the creation array. if ( untrailingslashit( $to ) === $dir ) { // Skip over the working directory, we know this exists (or will exist). continue; } if ( ! str_contains( $dir, $to ) ) { // If the directory is not within the working directory, skip it. continue; } $parent_folder = dirname( $dir ); while ( ! empty( $parent_folder ) && untrailingslashit( $to ) !== $parent_folderChangelog
Introduced in 3.0.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.0.4 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.