PHP 方法来查找网络服务器的临时路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1520956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP way to find the web server's temp path?
提问by Petruza
When you handle an HTTP upload the file is uploaded to
当您处理 HTTP 上传文件时,文件被上传到
$_FILES['field_name']['tmp_name']
I know I could extract the temp path from there. But I was expecting maybe a $_SERVERparam that had the temp path (there's none) or other elegant way of knowing it.
我知道我可以从那里提取临时路径。但我期待可能有一个$_SERVER具有临时路径(没有)或其他优雅方式的参数。
Is there any?
有没有?
回答by Waleed Amjad
ini_get('upload_tmp_dir');
回答by Ionu? G. Stan
The function sys_get_temp_dir()returns the directory path used by PHP to store temporary files.
该函数sys_get_temp_dir()返回 PHP 用来存储临时文件的目录路径。
回答by Nate Glenn
Using XAMPP on my private computer, the ini_getmethod worked great, since php.ini specifies the value upload_tmp_dir. However, after uploading to my employer's server, this code didn't work because the value apparently didn't exist in his server's php.ini file. So, I used the system's temp dir as the default, and now the script works on my computer and on his server:
在我的私人计算机上使用 XAMPP,该ini_get方法效果很好,因为 php.ini 指定了值upload_tmp_dir。但是,上传到我雇主的服务器后,此代码不起作用,因为该值显然不存在于他的服务器的 php.ini 文件中。因此,我使用系统的临时目录作为默认目录,现在该脚本可以在我的计算机和他的服务器上运行:
$ini_val = ini_get('upload_tmp_dir');
$upload_tmp_dir = $ini_val ? $ini_val : sys_get_temp_dir();
回答by Kevin Andrews
I personally would use Sbm007's suggestion of:
我个人会使用 Sbm007 的建议:
ini_get('upload_tmp_dir');
ini_get('upload_tmp_dir');
As this takes into account apache per virtualhost settings like the upload_tmp_dir and open_basedir restrictions, where as :
因为这考虑了每个虚拟主机的 apache 设置,例如 upload_tmp_dir 和 open_basedir 限制,其中:
sys_get_temp_dir()
sys_get_temp_dir()
Would only return the OS's specific temp directory, and if inside a multi-hosted environment could give you a directory you can't write to.
只会返回操作系统的特定临时目录,如果在多主机环境中可以为您提供一个无法写入的目录。

