wp_tempnam()
云策文档标注
概述
wp_tempnam() 函数用于生成一个临时唯一文件的文件名,基于传入的参数或默认值创建,并确保文件可写。调用者需负责后续的文件删除或移动操作。
关键要点
- 返回一个临时唯一文件的文件名,基于传入的 $filename 和 $dir 参数,或使用默认值(如当前 Unix 时间戳和可写临时目录)。
- 调用函数必须自行删除或移动生成的文件,以避免残留。
- 函数内部处理文件名冲突、长度限制(如 255 字符限制)和文件创建,确保返回可写路径。
- 相关函数包括 wp_generate_password()、get_temp_dir() 和 wp_unique_filename(),用于辅助生成随机数据、获取临时目录和确保文件名唯一。
代码示例
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;
}注意事项
- 函数不会自动删除文件,调用者需在完成后手动处理文件清理。
- 不支持文件扩展名,临时文件默认使用 .tmp 后缀,但可通过自定义函数(如用户贡献的 wporg_k_tempnam)实现扩展名支持和自动删除。
- 确保传入的目录可写,否则可能影响文件创建。
原文内容
Returns a filename of a temporary unique file.
Description
Please note that the calling function must delete or move the file.
The filename is based off the passed parameter or defaults to the current unix timestamp, while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory.
Parameters
$filenamestringoptional-
Filename to base the Unique file off. Default empty.
$dirstringoptional-
Directory to store the file in. Default empty.
Source
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;
}
Changelog
| Version | Description |
|---|---|
| 2.6.0 | Introduced. |
Skip to note 2 content
Nabil
Keep in mind that (1) the function doens’t delete the file and that (2) you can’t use extensions, but you can use my function which supports extensions and deletes files at the end of the script execution:
Examples:
// Basic $filename = 'hello.txt'; $content = 'Hello World!'; $file = wporg_k_tempnam( $file, $content ); // Better - salt in the file name $filename = 'hello-' . wp_generate_password( 5, false ) . '.txt'; $content = 'Hello World!'; $file = wporg_k_tempnam( $file, $content );The function:
/** * Create temporary file in system temporary directory. * * @author Nabil Kadimi - <a href="https://kadimi.com" rel="nofollow ugc">https://kadimi.com</a> * * @param string $name File name. * @param string $content File contents. * @return string File path. */ function wporg_k_tempnam( $name, $content ) { $sep = DIRECTORY_SEPARATOR; $file = $sep . trim( sys_get_temp_dir(), $sep ) . $sep . ltrim( $name, $sep ); file_put_contents( $file, $content ); register_shutdown_function( function() use( $file ) { @unlink( $file ); } ); return $file; }