wp_zip_file_is_valid( string $file ): bool
- Since
- 6.4.4
- Source
wp-admin/includes/file.php:1576
Description
This function does not test to ensure that a file exists. Non-existent files are not valid ZIPs, so those will also return false.
Compatibility
- WordPress
- since 6.4.4
- 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 to the ZIP file.
Return value
bool- Whether the file is a valid ZIP file.
Performance profile
How much work a call to wp_zip_file_is_valid() 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
- Trivial
- Scaling
- Constant
- Instructions
- 15–31
- Plugin surface
- 1 hook
- Called by
- 1
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 34.
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
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_zip_file_is_valid() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 15 | ->properties() |
!apply_filters() | 20 | apply_filters(), ->properties() |
apply_filters() && $archive_is_valid === true | 22 | apply_filters(), ->open(), ->close() |
apply_filters() && $archive_is_valid !== true | 31 | apply_filters(), ->open(), ->properties() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 34 | 15–31 | 3 | |
| 8.5 | 34 | 15–31 | 3 | |
| 8.4 | 34 | 15–31 | 3 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 37 | 18–34 | 3 | |
| 8.2 | 37 | 18–34 | 3 | |
| 8.1 | 37 | 18–34 | 3 | |
| 7.4 | 37 | 18–34 | 3 |
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 wp_zip_file_is_valid() runs, in this order:
- apply_filters( unzip_file_use_ziparchive )filterline 1578 (+2 into the body)
Filters whether to use ZipArchive to unzip archives.
Uses · 3
- apply_filters()Calls the callback functions that have been added to a filter hook.
- ZipArchive::__construct()
- PclZip::__construct()
Used by · 1
- File_Upload_Upgrader::__construct()Construct the upgrader for a form.
Source code
function wp_zip_file_is_valid( $file ) { /** This filter is documented in wp-admin/includes/file.php */ if ( class_exists( 'ZipArchive', false ) && apply_filters( 'unzip_file_use_ziparchive', true ) ) { $archive = new ZipArchive(); $archive_is_valid = $archive->open( $file, ZipArchive::CHECKCONS ); if ( true === $archive_is_valid ) { $archive->close(); return true; } } // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file. require_once ABSPATH . 'wp-admin/includes/class-pclzip.php'; $archive = new PclZip( $file ); $archive_is_valid = is_array( $archive->properties() ); return $archive_is_valid;}Changelog
Introduced in 6.4.4. 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.