wp_filesize( string $path ): int
- Since
- 6.0.0
- Source
wp-includes/functions.php:3611
Compatibility
- WordPress
- since 6.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
$pathstring- Path to the file.
Return value
int- The size of the file in bytes, or 0 in the event of an error.
Performance profile
How much work a call to wp_filesize() 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
- Heavy
- Scaling
- Constant
- Instructions
- 10–26
- Plugin surface
- 2 hooks
- Called by
- 8
Reads or writes the filesystem via file_exists().
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 28.
Third-party callbacks on 'pre_wp_filesize', 'wp_filesize' run inside this call, and their cost is not bounded by anything here.
8 places 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 - filesystemfilesystem access
file_exists()called directly
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_filesize() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_int($size) | 10 | apply_filters() |
!is_int($size) && !file_exists() | 22 | apply_filters(), file_exists(), apply_filters() |
!is_int($size) && file_exists() | 26 | apply_filters(), file_exists(), filesize(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 28 | 10–26 | 2 | |
| 8.5 | 28 | 10–26 | 2 | |
| 8.4 | 28 | 10–26 | 2 | |
| 8.3 | 28 | 10–26 | 2 | |
| 8.2 | 28 | 10–26 | 2 | |
| 8.1 | 28 | 10–26 | 2 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 29 | 10–27 | 2 |
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 wp_filesize() runs, in this order:
- apply_filters( pre_wp_filesize )filterline 3620 (+9 into the body)
Filters the result of wp_filesize before the PHP function is run.
Uses · 1
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 8
- WP_Image_Editor_GD::_save()
- WP_Image_Editor_Imagick::_save()
- _wp_image_meta_replace_original()Updates the attached file and image meta data when the original image was edited.
- attachment_submitbox_metadata()Displays non-editable attachment metadata in the publish meta box.
- wp_create_image_subsizes()Creates image sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata.
- wp_generate_attachment_metadata()Generates attachment meta data and create image sub-sizes for images.
- wp_maybe_inline_styles()Allows small styles to be inlined.
- wp_prepare_attachment_for_js()Prepares an attachment post object for JS, where it is expected to be JSON-encoded and fit into an Attachment model.
Source code
function wp_filesize( $path ) { /** * Filters the result of wp_filesize before the PHP function is run. * * @since 6.0.0 * * @param null|int $size The unfiltered value. Returning an int from the callback bypasses the filesize call. * @param string $path Path to the file. */ $size = apply_filters( 'pre_wp_filesize', null, $path ); if ( is_int( $size ) ) { return $size; } $size = file_exists( $path ) ? (int) filesize( $path ) : 0; /** * Filters the size of the file. * * @since 6.0.0 * * @param int $size The result of PHP filesize on the file. * @param string $path Path to the file. */ return (int) apply_filters( 'wp_filesize', $size, $path );}Changelog
Introduced in 6.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 6.9.7 tag, from
src/wp-includes/functions.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.