# wp_tempnam()

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

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

## Сигнатура

```php
wp_tempnam( string $filename = '', string $dir = '' ): string
```

## Описание

Обратите внимание, что вызывающая функция должна удалить или переместить файл.
Имя файла формируется на основе переданного параметра либо по умолчанию из текущей временной метки Unix, а каталог можно указать явно или, оставив его пустым, использовать по умолчанию доступный для записи временный каталог.

## Параметры

- `$filename` `string` — необязательный, по умолчанию `''`. Имя файла, на основе которого формируется уникальный файл.
- `$dir` `string` — необязательный, по умолчанию `''`. Каталог для хранения файла.

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

`string`

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

Файл: `wp-admin/includes/file.php:673`

```php
function wp_tempnam( $filename = '', $dir = '' ) {
	if ( empty( $dir ) ) {
		$dir = get_temp_dir();
	}

	if ( empty( $filename ) || in_array( $filename, array( '.', '/', '\\' ), true ) ) {
		$filename = uniqid();
	}

	// Use the basename of the given file without the extension as the name for the temporary directory.
	$temp_filename = basename( $filename );
	$temp_filename = preg_replace( '|\.[^.]*$|', '', $temp_filename );

	// If the folder is falsey, use its parent directory name instead.
	if ( ! $temp_filename ) {
		return wp_tempnam( dirname( $filename ), $dir );
	}

	// Suffix some random data to avoid filename conflicts.
	$temp_filename .= '-' . wp_generate_password( 6, false );
	$temp_filename .= '.tmp';
	$temp_filename  = wp_unique_filename( $dir, $temp_filename );

	/*
	 * Filesystems typically have a limit of 255 characters for a filename.
	 *
	 * If the generated unique filename exceeds this, truncate the initial
	 * filename and try again.
	 *
	 * As it's possible that the truncated filename may exist, producing a
	 * suffix of "-1" or "-10" which could exceed the limit again, truncate
	 * it to 252 instead.
	 */
	$characters_over_limit = strlen( $temp_filename ) - 252;
	if ( $characters_over_limit > 0 ) {
		$filename = substr( $filename, 0, -$characters_over_limit );
		return wp_tempnam( $filename, $dir );
	}

	$temp_filename = $dir . $temp_filename;

	$fp = @fopen( $temp_filename, 'x' );

	if ( ! $fp && is_writable( $dir ) && file_exists( $temp_filename ) ) {
		return wp_tempnam( $filename, $dir );
	}

	if ( $fp ) {
		fclose( $fp );
	}

	return $temp_filename;
}
```

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

- 2.6.0 — Introduced.

## Связанные

Использует: [`wp_tempnam`](https://chugunov.pro/api-wordpress/functions/wp_tempnam/), [`wp_generate_password`](https://chugunov.pro/api-wordpress/functions/wp_generate_password/), [`get_temp_dir`](https://chugunov.pro/api-wordpress/functions/get_temp_dir/), [`wp_unique_filename`](https://chugunov.pro/api-wordpress/functions/wp_unique_filename/).
Используется в: `WP_Customize_Manager::import_theme_starter_content`, `WP_REST_Attachments_Controller::upload_from_data`, `WP_Filesystem_FTPext::get_contents`, `WP_Filesystem_FTPext::put_contents`, `WP_Filesystem_ftpsockets::get_contents`, `WP_Filesystem_ftpsockets::put_contents`, [`wp_tempnam`](https://chugunov.pro/api-wordpress/functions/wp_tempnam/), [`download_url`](https://chugunov.pro/api-wordpress/functions/download_url/).

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