Skip to content

Commit 475a644

Browse files
ALeX Kaziksmalyshev
authored andcommitted
Implemented Feature #60524 (sys_temp_dir)
Added a new configuration directive which allows it to change the temporary directory, the default behavior is unchanged. This is a useful option if you use all/some hosts inside of one .ini file with sections and want to change the temp dir per user (maybe it's not allowed to write outside the users home directory). Since the TMPDIR variable affects the whole php that way can not be used for this scenario. (see https://bugs.php.net/bug.php?id=60524)
1 parent a9d013b commit 475a644

File tree

6 files changed

+33
-0
lines changed

6 files changed

+33
-0
lines changed

main/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ PHP_INI_BEGIN()
521521
STD_PHP_INI_ENTRY("default_mimetype", SAPI_DEFAULT_MIMETYPE, PHP_INI_ALL, OnUpdateString, default_mimetype, sapi_globals_struct,sapi_globals)
522522
STD_PHP_INI_ENTRY("error_log", NULL, PHP_INI_ALL, OnUpdateErrorLog, error_log, php_core_globals, core_globals)
523523
STD_PHP_INI_ENTRY("extension_dir", PHP_EXTENSION_DIR, PHP_INI_SYSTEM, OnUpdateStringUnempty, extension_dir, php_core_globals, core_globals)
524+
STD_PHP_INI_ENTRY("sys_temp_dir", NULL, PHP_INI_SYSTEM, OnUpdateStringUnempty, sys_temp_dir, php_core_globals, core_globals)
524525
STD_PHP_INI_ENTRY("include_path", PHP_INCLUDE_PATH, PHP_INI_ALL, OnUpdateStringUnempty, include_path, php_core_globals, core_globals)
525526
PHP_INI_ENTRY("max_execution_time", "30", PHP_INI_ALL, OnUpdateTimeout)
526527
STD_PHP_INI_ENTRY("open_basedir", NULL, PHP_INI_ALL, OnUpdateBaseDir, open_basedir, php_core_globals, core_globals)

main/php_globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ struct _php_core_globals {
8585
char *open_basedir;
8686
char *extension_dir;
8787
char *php_binary;
88+
char *sys_temp_dir;
8889

8990
char *upload_tmp_dir;
9091
long upload_max_filesize;

main/php_open_temporary_file.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,21 @@ PHPAPI const char* php_get_temporary_directory(void)
196196
return temporary_directory;
197197
}
198198

199+
/* Is there a temporary directory "sys_temp_dir" in .ini defined? */
200+
{
201+
char *sys_temp_dir = PG(sys_temp_dir);
202+
if (sys_temp_dir) {
203+
int len = strlen(sys_temp_dir);
204+
if (len >= 2 && sys_temp_dir[len - 1] == DEFAULT_SLASH) {
205+
temporary_directory = zend_strndup(sys_temp_dir, len - 1);
206+
return temporary_directory;
207+
} else if (len >= 1 && sys_temp_dir[len - 1] != DEFAULT_SLASH) {
208+
temporary_directory = zend_strndup(sys_temp_dir, len);
209+
return temporary_directory;
210+
}
211+
}
212+
}
213+
199214
#ifdef PHP_WIN32
200215
/* We can't count on the environment variables TEMP or TMP,
201216
* and so must make the Win32 API call to get the default

php.ini-development

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,10 @@ user_dir =
729729
; On windows:
730730
; extension_dir = "ext"
731731

732+
; Directory where the temporary files should be placed.
733+
; Defaults to the system default (see sys_get_temp_dir)
734+
; sys_temp_dir = "/tmp"
735+
732736
; Whether or not to enable the dl() function. The dl() function does NOT work
733737
; properly in multithreaded servers, such as IIS or Zeus, and is automatically
734738
; disabled on them.

php.ini-production

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,10 @@ user_dir =
729729
; On windows:
730730
; extension_dir = "ext"
731731

732+
; Directory where the temporary files should be placed.
733+
; Defaults to the system default (see sys_get_temp_dir)
734+
; sys_temp_dir = "/tmp"
735+
732736
; Whether or not to enable the dl() function. The dl() function does NOT work
733737
; properly in multithreaded servers, such as IIS or Zeus, and is automatically
734738
; disabled on them.

tests/basic/req60524.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
Req #60524 (Specify temporary directory)
3+
--INI--
4+
sys_temp_dir=/path/to/temp/dir
5+
--FILE--
6+
<?php echo sys_get_temp_dir(); ?>
7+
--EXPECT--
8+
/path/to/temp/dir

0 commit comments

Comments
 (0)