Title: File::convertFileToBase64
Published: May 20, 2026
Last modified: August 20, 2026

---

# File::convertFileToBase64( string $filePath ): string

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wordpress-aiclient-files-dto-file/convertfiletobase64/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wordpress-aiclient-files-dto-file/convertfiletobase64/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wordpress-aiclient-files-dto-file/convertfiletobase64/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wordpress-aiclient-files-dto-file/convertfiletobase64/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wordpress-aiclient-files-dto-file/convertfiletobase64/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wordpress-aiclient-files-dto-file/convertfiletobase64/?output_format=md#wp--skip-link--target)

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Converts a local file to base64.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wordpress-aiclient-files-dto-file/convertfiletobase64/?output_format=md#parameters)󠁿

 `$filePath`stringrequired

The path to the local file.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wordpress-aiclient-files-dto-file/convertfiletobase64/?output_format=md#return)󠁿

 string The base64-encoded file data.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wordpress-aiclient-files-dto-file/convertfiletobase64/?output_format=md#source)󠁿

    ```php
    private function convertFileToBase64(string $filePath): string
    {
        $fileContent = @file_get_contents($filePath);
        if ($fileContent === \false) {
            throw new RuntimeException(sprintf('Unable to read file: %s', $filePath));
        }
        return base64_encode($fileContent);
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/php-ai-client/src/files/dto/file.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/php-ai-client/src/Files/DTO/File.php#L133)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/php-ai-client/src/Files/DTO/File.php#L133-L140)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wordpress-aiclient-files-dto-file/convertfiletobase64/?output_format=md#related)󠁿

| Used by | Description | 
| [File::detectAndProcessFile()](https://developer.wordpress.org/reference/classes/wordpress-aiclient-files-dto-file/detectandprocessfile/)`wp-includes/php-ai-client/src/Files/DTO/File.php` |

Detects the file type and processes it accordingly.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wordpress-aiclient-files-dto-file/convertfiletobase64/?output_format=md#changelog)󠁿

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

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwordpress-aiclient-files-dto-file%2Fconvertfiletobase64%2F)
before being able to contribute a note or feedback.