Title: WP_REST_Attachments_Controller::get_attachment_filesize
Published: May 20, 2026

---

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

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/get_attachment_filesize/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/get_attachment_filesize/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/get_attachment_filesize/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/get_attachment_filesize/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/get_attachment_filesize/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/get_attachment_filesize/?output_format=md#wp--skip-link--target)

Gets the attachment’s file size in bytes.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/get_attachment_filesize/?output_format=md#parameters)󠁿

 `$attachment_id`intrequired

Attachment ID.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/get_attachment_filesize/?output_format=md#return)󠁿

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

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/get_attachment_filesize/?output_format=md#source)󠁿

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

[View all references](https://developer.wordpress.org/reference/files/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php#L1843)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php#L1843-L1858)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/get_attachment_filesize/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_filesize()](https://developer.wordpress.org/reference/functions/wp_filesize/)`wp-includes/functions.php` |

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

  | 
| [wp_get_original_image_path()](https://developer.wordpress.org/reference/functions/wp_get_original_image_path/)`wp-includes/post.php` |

Retrieves the path to an uploaded image file.

  | 
| [wp_get_attachment_metadata()](https://developer.wordpress.org/reference/functions/wp_get_attachment_metadata/)`wp-includes/post.php` |

Retrieves attachment metadata for attachment ID.

  | 
| [get_attached_file()](https://developer.wordpress.org/reference/functions/get_attached_file/)`wp-includes/post.php` |

Retrieves attached file path based on attachment ID.

  |

| Used by | Description | 
| [WP_REST_Attachments_Controller::prepare_item_for_response()](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/prepare_item_for_response/)`wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php` |

Prepares a single attachment output for response.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/get_attachment_filesize/?output_format=md#changelog)󠁿

| Version | Description | 
| [7.0.0](https://developer.wordpress.org/reference/since/7.0.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_rest_attachments_controller%2Fget_attachment_filesize%2F)
before being able to contribute a note or feedback.