WP_REST_Attachments_Controller::get_attachment_filesizeprotectedWP 7.0.0

Gets the attachment's file size in bytes.

Method of the class: WP_REST_Attachments_Controller{}

No Hooks.

Returns

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

Usage

// protected - for code of main (parent) or child class
$result = $this->get_attachment_filesize( $attachment_id ): ?int;
$attachment_id(int) (required)
Attachment ID.

Changelog

Since 7.0.0 Introduced.

WP_REST_Attachments_Controller::get_attachment_filesize() code WP 7.0

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