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? Link to comment https://forums.phpfreaks.com/topic/34258-unique-file-id/ 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. Link to comment https://forums.phpfreaks.com/topic/34258-unique-file-id/#findComment-161128 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 Link to comment https://forums.phpfreaks.com/topic/34258-unique-file-id/#findComment-161145 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. Link to comment https://forums.phpfreaks.com/topic/34258-unique-file-id/#findComment-161157 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 Link to comment https://forums.phpfreaks.com/topic/34258-unique-file-id/#findComment-161174 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. Link to comment https://forums.phpfreaks.com/topic/34258-unique-file-id/#findComment-161237 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.