PHP Functions to Create Temporary Files
PHP provides the tmpfile() function to create temporary files. It accepts no parameters, it's a 1:1 mapping to the C tmpfile() function and it returns a file handle (rather than a pathname). It also deletes the file when the script ends. That might work fine in some cases, but it's not as flexible as it could be and it leaves the door open for a race condition.
Another option is the tempnam() function, which is available since PHP 5.2.1. It provides some more options, such as the path where the file is created and if the file should be writable or not. However, it still doesn't allow you to set the directory where the file is stored, which might be a bit inconvenient for some situations.
Another alternative is the sys_get_temp_dir() function, which provides the path of the directory where PHP stores temporary files by default. It's not part of the standard PHP API, but it can be used with a function call like this: