note 93390 modified in function.sys-get-temp-dir by danbrown

From: Date: Tue, 23 Jul 2013 13:50:58 +0000
Subject: note 93390 modified in function.sys-get-temp-dir by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-195732@lists.php.net to get a copy of this message
There's no need to use a random name for the directory for tempnam. Since a file and a directory can't share the same name on the filesystem, we can exploit this and simply use the name of the current file. It is guaranteed that the directory won't exist (because it's a file, of course). <?php 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; } } ?> --was-- There's no need to use a random name for the directory for tempnam. Since a file and a directory can't share the same name on the filesystem, we can exploit this and simply use the name of the current file. It is guaranteed that the directory won't exist (because it's a file, of course). Improving on the last post... <?php 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; } } ?> http://php.net/manual/en/function.sys-get-temp-dir.php

« previous php.notes (#195732) next »