I use this code in my project bloxool to find a temporary filename.
function bx_tmpfilename() { if(!function_exists('sys_get_temp_dir')) { if(defined('BLOXOOL_TEMP_DIR')) { $temp = BLOXOOL_TEMP_DIR; } else { $temp = getenv('TMP'); if($temp === FALSE || empty($temp)) { $temp = getenv('TEMP'); if($temp === FALSE || empty($temp)) { $temp = getenv('TMPDIR'); if($temp === FALSE || empty($temp)) { $temp = '/tmp/'; } } } } } else { $temp = sys_get_temp_dir(); } if(!is_writable($temp)) { bx_fatal_error(BX_F_ERR_NOTMP); exit(-1); } return tempnam($temp, 'BX'); }