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

---

# WP_REST_Attachments_Controller::get_attachment_filename( int $attachment_id ): string|null

## In this article

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

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

Gets the attachment’s original file name.

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

 `$attachment_id`intrequired

Attachment ID.

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

 string|null Attachment file name, or null if not found.

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

    ```php
    protected function get_attachment_filename( int $attachment_id ): ?string {
    	$path = wp_get_original_image_path( $attachment_id );

    	if ( $path ) {
    		return wp_basename( $path );
    	}

    	$path = get_attached_file( $attachment_id );

    	if ( $path ) {
    		return wp_basename( $path );
    	}

    	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#L1819)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php#L1819-L1833)

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

| Uses | Description | 
| [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.

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

Retrieves attached file path based on attachment ID.

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

i18n-friendly version of basename().

  |

[Show 1 more](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/get_attachment_filename/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/get_attachment_filename/?output_format=md#)

| 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_filename/?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_filename%2F)
before being able to contribute a note or feedback.