DonutReply DonutReply - 4 months ago 8x
PHP Question

PHP Can't find tmp directory

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

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

When debugging this error I found that
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

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']); }
if (file_exists($tempfile)) {
return realpath(dirname($tempfile));
return null;

But there is no reference to a tmp directory in the $_ENV array and
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


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.