tags:

views:

45

answers:

1

I need to create temporary files with PHP for my site. How can I have them numbered in the following manner:

tmp_file_01.jpg
tmp_file_02.jpg
tmp_file_XX.jpg

I'd like to avoid running the shell to figure out what the next available index is.

+3  A: 

You're going to find this is relatively difficult to achieve (note, I'm assuming that you're trying to avoid filesystem access, in addition to invoking a shell). If you're married to the idea of incrementing filenames, you'll probably want to use some sort of shared datastore for holding your sequence number, like mysql, memcached, or APC.

However, depending on what you're actually trying to do, you may be able to abandon the incrementing numbered scheme, and just use tempnam()

If you're not against filesystem access, there are lots of options that don't involve direct shell invocation. e.g., you can just glob('tmp_file_*.jpg')

Frank Farmer
Wonderful! tempnam() will do it.
Fortress