-
Windows 10 -
PHPStudy 8.1 集成环境 -
PHP 7.3.4 -
Apache 2.4.39
PHP中的临时文件目录从哪来?
upload_tmp_dir
这个配置项的说明:The temporary directory used for storing files when doing file upload. Must be writable by whatever user PHP is running as. If not specified PHP will use the system's default. If the directory specified here is not writable, PHP falls back to the system default temporary directory. If open_basedir is on, then the system default directory must be allowed for an upload to succeed.
upload_tmp_dir
用来指定文件上传时的临时目录;如果这个值没有配置,则使用系统默认的临时目录。<?php
var_dump(sys_get_temp_dir());
C:Windows
:sys_get_temp_dir
是怎么获取临时文件的:/* {{{ proto string sys_get_temp_dir()
Returns directory path used for temporary files */
PHP_FUNCTION(sys_get_temp_dir)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
RETURN_STRING((char *)php_get_temporary_directory());
}
/* }}} */
php_get_temporary_directory
方法呢:PHPAPI const char* php_get_temporary_directory(void)
{
/* Did we determine the temporary directory already? */
if (PG(php_sys_temp_dir)) {
return PG(php_sys_temp_dir);
}
/* Is there a temporary directory "sys_temp_dir" in .ini defined? */
{
char *sys_temp_dir = PG(sys_temp_dir);
if (sys_temp_dir) {
size_t len = strlen(sys_temp_dir);
if (len >= 2 && sys_temp_dir[len - 1] == DEFAULT_SLASH) {
PG(php_sys_temp_dir) = estrndup(sys_temp_dir, len - 1);
return PG(php_sys_temp_dir);
} else if (len >= 1 && sys_temp_dir[len - 1] != DEFAULT_SLASH) {
PG(php_sys_temp_dir) = estrndup(sys_temp_dir, len);
return PG(php_sys_temp_dir);
}
}
}
#ifdef PHP_WIN32
/* We can't count on the environment variables TEMP or TMP,
* and so must make the Win32 API call to get the default
* directory for temporary files. Note this call checks
* the environment values TMP and TEMP (in order) first.
*/
{
wchar_t sTemp[MAXPATHLEN];
char *tmp;
size_t len = GetTempPathW(MAXPATHLEN, sTemp);
if (!len) {
return NULL;
}
if (NULL == (tmp = php_win32_ioutil_conv_w_to_any(sTemp, len, &len))) {
return NULL;
}
PG(php_sys_temp_dir) = estrndup(tmp, len - 1);
free(tmp);
return PG(php_sys_temp_dir);
}
#else
// ...
#endif
}
-
首先查看php.ini中是否有定义 sys_temp_dir
这个配置项,如果有则返回之 -
如果当前环境是WIN32,则调用系统API(GetTempPathW)来获取临时目录
sys_temp_dir
,发现也没有进行配置:C:Windows
这个值来自于Windows API GetTempPathW。为什么PHP获取不到我配置的环境变量
TEMP
环境变量,然后PHP-CGI自己也没设置这个环境变量,就导致最后获取到的是fallback的一个值。如何解决这个问题
<IfModule fcgid_module>
FcgidInitialEnv TEMP "C:windowsTemp"
</IfModule>
sys_temp_dir
设置成需要的值即可:sys_temp_dir = C:windowsTemp
sys_get_temp_dir
结果,正常了:本文始发于微信公众号(代码审计):记一次排查PHP上传目录配置的经历
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论