'Do I need to cleanup files created by tempnam() myself?

I created a bunch of tempnam() files for use during my request. Do I need to specifically unlink() them when they're not needed anymore, or will PHP / the system take care of it themselves?



Solution 1:[1]

Files created by tempnam() will not be deleted by PHP.

You'll have to unlink() them manually.


If you need a temporary file thats automatically deleted by PHP, consider using tmpfile():

Creates a temporary file

The file is automatically removed when closed (for example, by calling fclose(), or when there are no remaining references to the file handle returned by tmpfile()), or when the script ends.

Solution 2:[2]

Yes, unlink (delete) temp files when you're done with them. Some, but not all, OSs clean up their temp directories once in a while. But tempname() uses directories other than the OS temp directories.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 0stone0
Solution 2 O. Jones