public static function secureTmpname($dir = null, $prefix = 'tmp', $postfix = '.tmp')
    {
        // validate arguments
        if (!(isset ($postfix) && is_string($postfix)))
            return false;

        if (!(isset ($prefix) && is_string($prefix)))
            return false;

        if (!isset ($dir))
            $dir = getcwd();

        // find a temporary name
        $tries = 1;
        do {
            // get a known, unique temporary file name
            $sysFileName = tempnam($dir, $prefix);
            if ($sysFileName === false)
                return false;

            // tack on the extension
            $newFileName = $sysFileName . $postfix;
            if ($sysFileName == $newFileName)
                return $sysFileName;

            // move or point the created temporary file to the new filename
            // NOTE: these fail if the new file name exist
            $newFileCreated = @rename($sysFileName, $newFileName);
            if ($newFileCreated)
                return $newFileName;

            unlink($sysFileName);
            $tries++;
        } while ($tries <= 5);

        return false;
    }
public static function secureTmpname($dir = null, $prefix = 'tmp', $postfix = '.tmp')
    {
        // validate arguments
        if (!(isset ($postfix) && is_string($postfix)))
            return false;

        if (!(isset ($prefix) && is_string($prefix)))
            return false;

        if (!isset ($dir))
            $dir = getcwd();

        // find a temporary name
        $tries = 1;
        do {
            // get a known, unique temporary file name
            $sysFileName = tempnam($dir, $prefix);
            if ($sysFileName === false)
                return false;

            // tack on the extension
            $newFileName = $sysFileName . $postfix;
            if ($sysFileName == $newFileName)
                return $sysFileName;

            // move or point the created temporary file to the new filename
            // NOTE: these fail if the new file name exist
            $newFileCreated = @rename($sysFileName, $newFileName);
            if ($newFileCreated)
                return $newFileName;

            unlink($sysFileName);
            $tries++;
        } while ($tries <= 5);

        return false;
    }
public static function secureTmpname ( $ dir = null , $ prefix = 'tmp' , $ postfix = '.tmp' ) { // validate arguments if ( ! ( isset ( $ postfix ) && is_string ( $ postfix ) ) ) return false ; if ( ! ( isset ( $ prefix ) && is_string ( $ prefix ) ) ) return false ; if ( ! isset ( $ dir ) ) $ dir = getcwd ( ) ; // find a temporary name $ tries = 1 ; do { // get a known, unique temporary file name $ sysFileName = tempnam ( $ dir , $ prefix ) ; if ( $ sysFileName === false ) return false ; // tack on the extension $ newFileName = $ sysFileName . $ postfix ; if ( $ sysFileName == $ newFileName ) return $ sysFileName ; // move or point the created temporary file to the new filename // NOTE: these fail if the new file name exist $ newFileCreated = @ rename ( $ sysFileName , $ newFileName ) ; if ( $ newFileCreated ) return $ newFileName ; unlink ( $ sysFileName ) ; $ tries ++ ; } while ( $ tries <= 5 ) ; return false ; }
Creates a temp file with a specific extension.

Creating a temporary file with a specific extension is a common
requirement on dynamic websites. Largely this need arises from
Microsoft browsers that identify a downloaded file's mimetype based
on the file's extension.

No single PHP function creates a temporary filename with a specific
extension, and, as has been shown, there are race conditions involved
unless you use the PHP atomic primitives.

I use only primitives below and exploit OS dependent behaviour to
securely create a file with a specific postfix, prefix, and directory.

@param string $dir     Optional. The directory in which to create the new file
@param string $prefix  Default 'tmp'. The prefix of the generated temporary filename.
@param string $postfix Default '.tmp'. The extension or postfix for the generated filename.

@see    http://us.php.net/manual/en/function.tempnam.php#42052
@author bishop

@return string the filename of the new file
Creates a temp file with a specific extension .