wppaste
WordPress

wp_filesize( string $path ): int

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

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

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
10–26

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

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
8

8 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)10apply_filters()
!is_int($size) && !file_exists()22apply_filters(), file_exists(), apply_filters()
!is_int($size) && file_exists()26apply_filters(), file_exists(), filesize(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev2810–262
8.52810–262
8.42810–262
8.32810–262
8.22810–262
8.12810–2621 fewer instruction than PHP 7.4
7.42910–272

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 3620 (+9 into the body)

    Filters the result of wp_filesize before the PHP function is run.

  2. apply_filters( wp_filesize )filterline 3636 (+25 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 · 8

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.

  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.

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.