百科狗-知识改变命运!
--

tempnam() - 建立一个具有唯一文件名的文件 - php 文件目录函数

百变鹏仔1年前 (2023-11-21)阅读数 28#技术干货
文章标签文件

tempnam()

(PHP 4, PHP 5, PHP 7)

建立一个具有唯一文件名的文件

说明

tempnam(string $dir,string $prefix): string

tempnam() - 建立一个具有唯一文件名的文件 - php 文件目录函数

在指定目录中建立一个具有唯一文件名的文件。如果该目录不存在,tempnam()会在系统临时目录中生成一个文件,并返回其文件名。

参数

$dir

The directory where the temporary filename will be created.

$prefix

产生临时文件的前缀。

Note: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 版中改变了。也会建立一个临时文件以避免竞争情形,即有可能会在产生出作为文件名的字符串与脚本真正建立该文件之间会在文件系统中存在同名文件。注意,如果不再需要该文件则要删除此文件,不会自动删除的。

范例

Example #1tempnam()例子

注释

Note:如果 PHP 不能在指定的$dir参数中创建文件,则退回到系统默认值。在 NTFS 文件系统中,同样的情况也发生在$dir中文件数超过 65534 个的时候。

参见

  • tmpfile() 建立一个临时文件
  • sys_get_temp_dir() 返回用于临时文件的目录
  • unlink() 删除文件
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: 
Please note that this function might throw a notice in PHP 7.1.0 and above. This was a bugfix: https://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.
Be careful with you forward and back slashes. Innocent looking code like this...
$uploaddir = "C:/Program Files/Apache Group/Apache2/htdocs/sasdap/uploads/";
$tempFile = tempnam ($uploaddir, "TMPANAL");
$fp = fopen($tmpfname, "w");
fwrite($fp, $iqdata);
//fclose($fp);
... may show something odd when echoing $tempFile";
i.e. /Program Files/Apache Group/Apache2/htdocs/sasdap/uploads/\TMP3D.tmp
                            
Must... remember... to... use... backslashes...
 - Lee P. Reilly
Note that tempnam returns the full path to the temporary file, not just the filename.
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)
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).
Notice that tempnam will return NULL (not false) if the second parameter  isn't transferred:

also the warning will be generated:
Warning: tempnam() expects exactly 2 parameters, 1 given in php shell code ...
Beware that on Windows NT and other windows, if you have, for example, a variable $work_dir with a path to some dir on your document root(or any other dir). Note the following: 
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:

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)