WP_REST_Attachments_Controller::get_attachment_filesize( int $attachment_id ): int|null

In this article

Gets the attachment’s file size in bytes.

Parameters

$attachment_idintrequired
Attachment ID.

Return

int|null Attachment file size in bytes, or null if not available.

Source

protected function get_attachment_filesize( int $attachment_id ): ?int {
	$meta = wp_get_attachment_metadata( $attachment_id );

	if ( isset( $meta['filesize'] ) ) {
		return $meta['filesize'];
	}

	$original_path = wp_get_original_image_path( $attachment_id );
	$attached_file = $original_path ? $original_path : get_attached_file( $attachment_id );

	if ( is_string( $attached_file ) && is_readable( $attached_file ) ) {
		return wp_filesize( $attached_file );
	}

	return null;
}

Changelog

VersionDescription
7.0.0Introduced.

User Contributed Notes

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