Using sys_get_temp_dir() in PHP < 5.2.1

December 30, 2009

I wanted to use the PHPExcel package on my Debian system running 5.2.0, however PHPExcel 1.7.1 requires the sys_get_temp_dir() function which doesn’t exist prior to PHP 5.2.1.  Of course aside from updating PHP, we would need to replicate the functionality of sys_get_temp_dir().  This gem in the php.net comments on the function provides a workaround.

if ( !function_exists('sys_get_temp_dir'))
{
	function sys_get_temp_dir()
	{
		if( $temp=getenv('TMP') )        return $temp;
		if( $temp=getenv('TEMP') )        return $temp;
		if( $temp=getenv('TMPDIR') )    return $temp;
		$temp=tempnam(__FILE__,'');
		if (file_exists($temp))
		{
			unlink($temp);
			return dirname($temp);
		}
		return null;
	}
}

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive