arkum Posted January 15, 2007 Share Posted January 15, 2007 Howdy,What is the most sensible way of giving any file which I try to save (which doesn't already exist) a unique name? Should I append the date to a consistant filename? Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted January 15, 2007 Share Posted January 15, 2007 a timestamp may be more usefull - or even better micro time. If multiple users can upload then just to be really careful perhaps use their user_id and the micro-time to name the file. Quote Link to comment Share on other sites More sharing options...
arkum Posted January 15, 2007 Author Share Posted January 15, 2007 Hi ToonMariner,Thanks for replying, but can you explain how I would append a timestamp? I don't have to worry about multiple users, so it can be very simple.Thanks Quote Link to comment Share on other sites More sharing options...
Orio Posted January 15, 2007 Share Posted January 15, 2007 Using tempnam(), give this a try:[code]<?phpfunction unique_name(){ $name = tempnam("/", ""); if($name !== FALSE) { unlink($name); return $name; } else return FALSE;}?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
arkum Posted January 15, 2007 Author Share Posted January 15, 2007 Hi Orio,Just got sucked into your avatar animation for about 5 minutes(years?)...Don't quite get the code. You are creating a file, then deleteing it, then returning it?And what do the ("/", "") mean?Thanks Quote Link to comment Share on other sites More sharing options...
Orio Posted January 15, 2007 Share Posted January 15, 2007 Read about [url=http://www.php.net/tempnam]tempnam()[/url]. Here's what it does, basically:It creates a file within the given folder (I've set it "/" - the current folder) with the prefix given (I've set it to "" - no prefix). This new file has a name that is unique- there's no chance for over writing. This function returns the name of that file created, so you can use it. Back to my function- If an error occurs the function returns FALSE, and if everything is ok the unique filename is being returned. I deleted the file created by tempnam, because there's no need for it, you just want the name, right?Orio. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.