# validate_file()

URL: https://chugunov.pro/api-wordpress/functions/validate_file/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 1.2.0.

## Сигнатура

```php
validate_file( string $file, string[] $allowed_files = array() ): int
```

## Описание

Возвращаемое значение 1 означает, что путь к файлу содержит обход каталогов.
Возвращаемое значение 2 означает, что путь к файлу содержит путь с буквой диска Windows.
Возвращаемое значение 3 означает, что файл отсутствует в списке разрешённых файлов.

## Параметры

- `$file` `string` — обязательный. Путь к файлу.
- `$allowed_files` `string[]` — необязательный, по умолчанию `array()`. Массив разрешённых файлов.

## Возвращаемое значение

`int`

## Исходный код

Файл: `wp-includes/functions.php:6282`

```php
function validate_file( $file, $allowed_files = array() ) {
	if ( ! is_scalar( $file ) || '' === $file ) {
		return 0;
	}

	// Normalize path for Windows servers.
	$file = wp_normalize_path( $file );
	// Normalize path for $allowed_files as well so it's an apples to apples comparison.
	$allowed_files = array_map( 'wp_normalize_path', $allowed_files );

	// `../` on its own is not allowed:
	if ( '../' === $file ) {
		return 1;
	}

	// More than one occurrence of `../` is not allowed:
	if ( preg_match_all( '#\.\./#', $file, $matches, PREG_SET_ORDER ) && ( count( $matches ) > 1 ) ) {
		return 1;
	}

	// `../` which does not occur at the end of the path is not allowed:
	if ( str_contains( $file, '../' ) && '../' !== mb_substr( $file, -3, 3 ) ) {
		return 1;
	}

	// Files not in the allowed file list are not allowed:
	if ( ! empty( $allowed_files ) && ! in_array( $file, $allowed_files, true ) ) {
		return 3;
	}

	// Absolute Windows drive paths are not allowed:
	if ( ':' === substr( $file, 1, 1 ) ) {
		return 2;
	}

	return 0;
}
```

## История изменений

- 1.2.0 — Introduced.

## Связанные

Использует: [`wp_normalize_path`](https://chugunov.pro/api-wordpress/functions/wp_normalize_path/).
Используется в: `WP_REST_Plugins_Controller::validate_plugin_param`, [`wp_edit_theme_plugin_file`](https://chugunov.pro/api-wordpress/functions/wp_edit_theme_plugin_file/), [`wp_ajax_delete_plugin`](https://chugunov.pro/api-wordpress/functions/wp_ajax_delete_plugin/), [`wp_ajax_update_plugin`](https://chugunov.pro/api-wordpress/functions/wp_ajax_update_plugin/), [`validate_plugin`](https://chugunov.pro/api-wordpress/functions/validate_plugin/), [`validate_file_to_edit`](https://chugunov.pro/api-wordpress/functions/validate_file_to_edit/), [`download_url`](https://chugunov.pro/api-wordpress/functions/download_url/), [`_unzip_file_ziparchive`](https://chugunov.pro/api-wordpress/functions/_unzip_file_ziparchive/), [`_unzip_file_pclzip`](https://chugunov.pro/api-wordpress/functions/_unzip_file_pclzip/), `WP_Customize_Manager::__construct`, [`wp_get_active_and_valid_plugins`](https://chugunov.pro/api-wordpress/functions/wp_get_active_and_valid_plugins/), [`get_single_template`](https://chugunov.pro/api-wordpress/functions/get_single_template/), [`get_page_template`](https://chugunov.pro/api-wordpress/functions/get_page_template/), [`wp_get_active_network_plugins`](https://chugunov.pro/api-wordpress/functions/wp_get_active_network_plugins/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/validate_file/
