wp_filesize( string $path ): int

In this article

Wrapper for PHP filesize with filters and casting the result as an integer.

Parameters

$pathstringrequired
Path to the file.

Return

int The size of the file in bytes, or 0 in the event of an error.

Source

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 );
}

Hooks

apply_filters( ‘pre_wp_filesize’, null|int $size, string $path )

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

apply_filters( ‘wp_filesize’, int $size, string $path )

Filters the size of the file.

Changelog

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

User Contributed Notes

You must log in before being able to contribute a note or feedback.