[PHP 4, PHP 5, PHP 7, PHP 8]
tempnam — 建立一个具有唯一文件名的文件
说明
tempnam[string $dir
, string $prefix
]: string
参数
dir
The directory where the temporary filename will be created.
prefix
产生临时文件的前缀。
注意: Windows uses only the first three characters of prefix.
返回值
返回新的临时文件名,出错返回 false
。
更新日志
4.0.6 | 在 PHP 4.0.6 之前,tempnam[] 函数的行为取决于系统。在 Windows 下 TMP 环境变量会越过 dir 参数,在 Linux 下 TMPDIR 环境变量优先,而在 SVR4 下总是使用 dir 参数,如果其指向的目录存在的话。如果有疑问请参考系统文档中的 tempnam[3] 函数。
|
4.0.3 | 本函数的行为在 4.0.3 版中改变了。也会建立一个临时文件以避免竞争情形,即有可能会在产生出作为文件名的字符串与脚本真正建立该文件之间会在文件系统中存在同名文件。注意,如果不再需要该文件则要删除此文件,不会自动删除的。 |
范例
示例 #1 tempnam[] 例子
注释
注意: 如果 PHP 不能在指定的
dir
参数中创建文件,则退回到系统默认值。 在 NTFS 文件系统中,同样的情况也发生在dir
中文件数超过 65534 个的时候。
参见
- tmpfile[] - 建立一个临时文件
- sys_get_temp_dir[] - 返回用于临时文件的目录
- unlink[] - 删除文件
koyama ¶
13 years ago
Watch out using a blank $dir as a "trick" to create temporary files in the system temporary directory.
If an open_basedir restriction is in effect, the trick will not work. You will get a warning message like
Warning: tempnam[] [function.tempnam]: open_basedir restriction in effect.
File[] is not within the allowed path[s]: [/var/www/vhosts/example.com/httpdocs:/tmp]
What works is this:
stanislav dot eckert at vizson dot de ¶
5 years ago
Please note that this function might throw a notice in PHP 7.1.0 and above. This was a bugfix: //bugs.php.net/bug.php?id=69489
You can place an address operator [@] to sillence the notice:
Or you could try to set the "upload_tmp_dir" setting in your php.ini to the temporary folder path of your system. Not sure, if the last one prevents the notices.
Sebastian Kun ¶
17 years ago
If you go to the linux man page for the C function tempnam[3], you will see at the end "Never use this function. Use mkstemp[3] instead." But php's tempnam[] function doesn't actually use tmpnam[3], so there's no problem [under Linux, it will use mkstemp[3] if it's available].
Artur Graniszewski ¶
12 years ago
tempnam[] function does not support custom stream wrappers registered by stream_register_wrapper[].
For example if you'll try to use tempnam[] on Windows platform, PHP will try to generate unique filename in %TMP% folder [usually: C:\WINDOWS\Temp] without any warning or notice.
ouputs:
--------------------
PHP 5.2.13
node exists: 1
node is writable: 1
node is dir: 1
tempnam in dir: C:\Windows\Temp\tmp1D03.tmp
If you want to create temporary file, you have to create your own function [which will probably use opendir[] and fopen[$filename, "x"] functions]
Jason Pell ¶
15 years ago
I want to guarantee that the file will be created in the specified directory or else the function should return FALSE, I have a simple function that works, but I am unsure if its a potential security issue.
function dir_tempnam[$dir, $prefix]
{
$real_dir_path = realpath[$dir];
if [substr[$real_dir_path, -1] != '/']
$real_dir_path .= '/';
$tempfile = tempnam[$real_dir_path, $prefix];
$name = basename[$tempfile];
if[is_file[$real_dir_path.$name]]
return $name;
else
{
@unlink[$name];
return FALSE;
}
}
This function returns just the name of the temporary file in the specified directory, or FALSE.
Obviously it could return the entire $tempfile, but in my case, I actually want the basename value seperate.
Ron Korving ¶
16 years ago
This function creates a temporary directory. The previous example given could bug if between the unlink[] and mkdir[] some process creates the same directory or file. This implementation is faster too.
anakin dot skyw at gmx dot de ¶
18 years ago
>Under UNIX [where you can rename onto an extant file and so I used link], you will have to remove both the link and the link's target.
Couldn't you do
and get the same semantics as the windows version?
bishop ¶
18 years ago
Creating a temporary file with a specific extension is a common requirement on dynamic websites. Largely this need arises from Microsoft browsers that identify a downloaded file's mimetype based on the file's extension.
No single PHP function creates a temporary filename with a specific extension, and, as has been shown, there are race conditions involved unless you use the PHP atomic primitives.
I use only primitives below and exploit OS dependent behaviour to securely create a file with a specific postfix, prefix, and directory. Enjoy.
The isWindows function is mostly left as an exercise for the reader. A starting point is below: