Title: File::detectAndProcessFile
Published: May 20, 2026

---

# File::detectAndProcessFile( string $file, string|null $providedMimeType )

## In this article

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

[ Back to top](https://developer.wordpress.org/reference/classes/wordpress-aiclient-files-dto-file/detectandprocessfile/?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.

Detects the file type and processes it accordingly.

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

 `$file`stringrequired

The file string to process.

`$providedMimeType`string|nullrequired

The explicitly provided MIME type.

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

    ```php
    private function detectAndProcessFile(string $file, ?string $providedMimeType): void
    {
        // Check if it's a URL
        if ($this->isUrl($file)) {
            $this->fileType = FileTypeEnum::remote();
            $this->url = $file;
            $this->mimeType = $this->determineMimeType($providedMimeType, null, $file);
            return;
        }
        // Data URI pattern.
        $dataUriPattern = '/^data:(?:([a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_+.]*\/[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_+.]*' . '(?:;[a-zA-Z0-9\-]+=[a-zA-Z0-9\-]+)*)?;)?base64,([A-Za-z0-9+\/]*={0,2})$/';
        // Check if it's a data URI.
        if (preg_match($dataUriPattern, $file, $matches)) {
            $this->fileType = FileTypeEnum::inline();
            $this->base64Data = $matches[2];
            // Extract just the base64 data
            $extractedMimeType = empty($matches[1]) ? null : $matches[1];
            $this->mimeType = $this->determineMimeType($providedMimeType, $extractedMimeType, null);
            return;
        }
        // Check if it's a local file path (before base64 check)
        if (file_exists($file) && is_file($file)) {
            $this->fileType = FileTypeEnum::inline();
            $this->base64Data = $this->convertFileToBase64($file);
            $this->mimeType = $this->determineMimeType($providedMimeType, null, $file);
            return;
        }
        // Check if it's plain base64
        if (preg_match('/^[A-Za-z0-9+\/]*={0,2}$/', $file)) {
            if ($providedMimeType === null) {
                throw new InvalidArgumentException('MIME type is required when providing plain base64 data without data URI format.');
            }
            $this->fileType = FileTypeEnum::inline();
            $this->base64Data = $file;
            $this->mimeType = new MimeType($providedMimeType);
            return;
        }
        throw new InvalidArgumentException('Invalid file provided. Expected URL, base64 data, or valid local file path.');
    }
    ```

[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.0/src/wp-includes/php-ai-client/src/Files/DTO/File.php#L73)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/php-ai-client/src/Files/DTO/File.php#L73-L111)

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

| Uses | Description | 
| [MimeType::__construct()](https://developer.wordpress.org/reference/classes/wordpress-aiclient-files-valueobjects-mimetype/__construct/)`wp-includes/php-ai-client/src/Files/ValueObjects/MimeType.php` |

Constructor.

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

Determines the MIME type from various sources.

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

Converts a local file to base64.

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

Checks if a string is a valid URL.

  |

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

Constructor.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wordpress-aiclient-files-dto-file/detectandprocessfile/?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%2Fdetectandprocessfile%2F)
before being able to contribute a note or feedback.