Lanselot Posted September 16, 2007 Share Posted September 16, 2007 I have a code that's supposed to work in the following way: Generate a random number, check if a file exists with that number as filename. If it exists, then sum one to the number until a filename with that number doesn't exist. $new_name = rand(0,99999); $ext = substr($origen,-4,4); //Retrieves file extension while (file_exists($base.'images/'.$new_name.$ext)) { $new_name++; } Plain simple... well, the problem is that every some thousands, I get duplicated filenames, which shouldn't be possible. Am I missing any scenario? Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/69516-php-and-file_exists-paranoia/ Share on other sites More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 Not that I can think, also checked integers in the manual and should be no probs there... unless your not showing all code and are making files and it's not finished creating before checking, but I assume not? Quote Link to comment https://forums.phpfreaks.com/topic/69516-php-and-file_exists-paranoia/#findComment-349306 Share on other sites More sharing options...
Lanselot Posted September 16, 2007 Author Share Posted September 16, 2007 Well, the above code is in a function, and right before it's called the following is executed: $rscimage = getimagesize($base.'images/'.$file); if ($rscimage[2] > 2) { //Delete if not GIF,JPG unlink($base.'images/'.$file); } $result = rand_rename($file,$rscimage); //That's the function above Also in the original code(not simplified for posting), there's an escaped variable among the filename, maybe it has something to do?. Scopes are ok too. Quote Link to comment https://forums.phpfreaks.com/topic/69516-php-and-file_exists-paranoia/#findComment-349317 Share on other sites More sharing options...
jscix Posted September 16, 2007 Share Posted September 16, 2007 You will always get duplicate randoms, no matter how high your source variation is, especially over enough random generations. You could sort-of fix this by prefixing your file name with the date/time.. Quote Link to comment https://forums.phpfreaks.com/topic/69516-php-and-file_exists-paranoia/#findComment-349326 Share on other sites More sharing options...
Lanselot Posted September 16, 2007 Author Share Posted September 16, 2007 You will always get duplicate randoms, no matter how high your source variation is, especially over enough random generations. You could sort-of fix this by prefixing your file name with the date/time.. Agree, but that's why I put that loop in there... Quote Link to comment https://forums.phpfreaks.com/topic/69516-php-and-file_exists-paranoia/#findComment-349329 Share on other sites More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 You will always get duplicate randoms That's surely irrelevant in this case? Quote Link to comment https://forums.phpfreaks.com/topic/69516-php-and-file_exists-paranoia/#findComment-349330 Share on other sites More sharing options...
jscix Posted September 16, 2007 Share Posted September 16, 2007 Well, I'm saying you could pretty much eliminate the loop by using date/time instead of random Quote Link to comment https://forums.phpfreaks.com/topic/69516-php-and-file_exists-paranoia/#findComment-349331 Share on other sites More sharing options...
jscix Posted September 16, 2007 Share Posted September 16, 2007 Also probably irrelevant but if you have an sql database, it's really easy to use a uniquely generated id for naming your images, everytime an image is uploaded, just add a new entry to your database, and wallah. a unique id. but since your doing it this way, you prolly not working with one? Quote Link to comment https://forums.phpfreaks.com/topic/69516-php-and-file_exists-paranoia/#findComment-349333 Share on other sites More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 What I was liking about this was that if files are also being removed (and there's a max), then the lower random's will fill the gaps? Quote Link to comment https://forums.phpfreaks.com/topic/69516-php-and-file_exists-paranoia/#findComment-349334 Share on other sites More sharing options...
Lanselot Posted September 16, 2007 Author Share Posted September 16, 2007 Yes, I could put it some other ways, it's just annoying that it doesn't work as should. Actually its purpose is to generate non-consecutive/non-pattern filenames as to make it hard to guess(not that anything bad happens if someone does). Again, just annoyed. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/69516-php-and-file_exists-paranoia/#findComment-349337 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.