wppaste
WordPress

wp_filesize( string $path ): int

Since
6.0.0, 7.1.0
Source
wp-includes/functions.php:3646
Wrapper for PHP filesize with filters and casting the result as an integer.

Compatibility

WordPress
since 7.1.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

Reads or writes the filesystem via file_exists().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
14–39

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 42.

Plugin surface
2 hooks

Third-party callbacks on 'pre_wp_filesize', 'wp_filesize' run inside this call, and their cost is not bounded by anything here.

Called by
11

11 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • filesystemfilesystem accessfile_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.

WhenInstructionsCalls it makes
is_int($size) && $size14–16apply_filters()
!file_exists()29–35apply_filters(), file_exists(), apply_filters()
file_exists()33–39apply_filters(), file_exists(), filesize(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev4214–395
8.54214–395
8.44214–3957 fewer instructions than PHP 8.3
8.34916–465
8.24916–465
8.14916–4651 fewer instruction than PHP 7.4
7.45016–475

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:

  1. apply_filters( pre_wp_filesize )filterline 3656 (+10 into the body)

    Filters the result of wp_filesize() before the file_exists() PHP function is run.

  2. apply_filters( wp_filesize )filterline 3675 (+29 into the body)

    Filters the size of the file.

Uses · 1

  • apply_filters()Calls the callback functions that have been added to a filter hook.

Used by · 11

Source code

function wp_filesize( $path ): int {	/**	 * Filters the result of wp_filesize() before the file_exists() PHP function is run.	 *	 * @since 6.0.0	 * @since 7.1.0 Negative values are now ignored, being treated the same as null. Numeric values are cast to integers.	 *	 * @param null|int $size The unfiltered value. Returning a non-negative number from the callback bypasses the filesize call.	 * @param string   $path Path to the file.	 */	$size = apply_filters( 'pre_wp_filesize', null, $path );	if ( is_numeric( $size ) ) {		$size = (int) $size;	}	if ( is_int( $size ) && $size >= 0 ) {		return $size;	} 	$size = file_exists( $path ) ? (int) filesize( $path ) : 0; 	/**	 * Filters the size of the file.	 *	 * @since 6.0.0	 * @since 7.1.0 The return value is now always zero or greater. Numeric values are cast to integers.	 *	 * @param int    $size The result of PHP filesize on the file.	 * @param string $path Path to the file.	 */	$size = apply_filters( 'wp_filesize', $size, $path );	if ( is_numeric( $size ) ) {		$size = (int) $size;	} else {		$size = 0;	}	return max( 0, $size );}

Changelog

Introduced in 6.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

7.1.0
The return value is now ensured to always be greater than or equal to zero.from the docblock
6.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 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.