DonutReply DonutReply - 6 months ago 21
PHP Question

PHP Can't find tmp directory

I am having problems with functions that create files in the tmp directory such as

tmpfile()
and
tempnam()
. They all seem to fail to write to tmp and return false.
upload_tmp_dir
is set in php ini and file uploads work fine.

When debugging this error I found that
sys_get_temp_dir()
gets the location of the tmp directory unfortunately it's not supported in my PHP version (5.1.6). I also saw that using the following method replaces the functionality of
sys_get_temp_dir()
:

if ( !function_exists('sys_get_temp_dir')) {
function sys_get_temp_dir() {
if (!empty($_ENV['TMP'])) { return realpath($_ENV['TMP']); }
if (!empty($_ENV['TMPDIR'])) { return realpath( $_ENV['TMPDIR']); }
if (!empty($_ENV['TEMP'])) { return realpath( $_ENV['TEMP']); }
$tempfile=tempnam(__FILE__,'');
if (file_exists($tempfile)) {
unlink($tempfile);
return realpath(dirname($tempfile));
}
return null;
}
}


But there is no reference to a tmp directory in the $_ENV array and
tempnam()
fails as I mentioned before.

Also open_basedir is not set which I've heard can cause similar problems

How can I find out where the tmp directory is or whether it is even set?

Is this a apache server configuration issue or a PHP one?

Thanks for your help

Answer

TMP, TEMP (and maybe TMPDIR) are valid on Windows only and usually pointing to C:\Windows\TEMP. On Linux default temp location is /tmp. To workaround this (works with tempnam() function) you can create a temp folder somewhere within your website space, specify appropriate access permissions and pass this as first parameter to the above function.

Not a great solution but better than nothing.