Welcome to Geeklog, Anonymous Tuesday, December 10 2024 @ 12:56 pm EST
Geeklog Forums
com_makesid
Status: Banned
machinari
Forum User
Full Member
Registered: 03/22/04
Posts: 1512
So there I was, renaming files in a directory at lightning fast speed and up comes this error that I have a duplicate file name. I thought, "not possible cuz I'm using COM_makesid to generate the filenames." Lo and behold, I was wrong. I had COM_makesid running about 30 times per second and the random number that follows the seconds had already come up once. Sure, it's not inconceivable, but I'm still shocked.
Guess I should slow down the script. ...but that just seems pointless unless I ensure that the loop runs only once per second--I don't want to do that. hmmm. I guess I could lower the odds of getting a duplicate with a bigger random number. Any thoughts?
Guess I should slow down the script. ...but that just seems pointless unless I ensure that the loop runs only once per second--I don't want to do that. hmmm. I guess I could lower the odds of getting a duplicate with a bigger random number. Any thoughts?
14
6
Quote
Status: offline
Blaine
Forum User
Moderator
Registered: 07/16/02
Posts: 1232
Location:Canada
You might want to look at microtime() or mt_rand(). One other idea is to use the basic rand() and add that to the COM_makesid value.
For Random files, I use this routine to create a random 10 character filename.
function ppRandomFilename() {
$length=10;
srand((double)microtime()*1000000);
$possible_charactors = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$string = "";
while(strlen($string)<$length) {
$string .= substr($possible_charactors, rand()%(strlen($possible_charactors)),1);
}
$string .= date('mdHms'); // Now add the numerical MonthDayHourSecond just to ensure no possible duplicate
return($string);
}
Geeklog components by PortalParts -- www.portalparts.com
For Random files, I use this routine to create a random 10 character filename.
Text Formatted Code
function ppRandomFilename() {
$length=10;
srand((double)microtime()*1000000);
$possible_charactors = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$string = "";
while(strlen($string)<$length) {
$string .= substr($possible_charactors, rand()%(strlen($possible_charactors)),1);
}
$string .= date('mdHms'); // Now add the numerical MonthDayHourSecond just to ensure no possible duplicate
return($string);
}
Geeklog components by PortalParts -- www.portalparts.com
9
9
Quote
Status: Banned
machinari
Forum User
Full Member
Registered: 03/22/04
Posts: 1512
Thanks Blaine. that looks good.
12
9
Quote
Status: Banned
machinari
Forum User
Full Member
Registered: 03/22/04
Posts: 1512
so, I went a different way. Still using COM_makesid, I just put the new name into an array and check the array after the next call to COM_makesid. Like so:
// check here for new filename to ensure no duplicates.
do {
$file_id = COM_makesid();
$filename = $file_id . '.' . $ext;
} while (!empty($db_files) && in_array($filename, $db_files));
//rename file
$newname = $dir . $filename;
$oldname = $dir . $file;
rename($oldname, $newname);
//add new filename to db_array here for checking on next loop
$db_files[] = $filename;
of course that all makes more sense if you see it in a loop. :wink:
Text Formatted Code
// check here for new filename to ensure no duplicates.
do {
$file_id = COM_makesid();
$filename = $file_id . '.' . $ext;
} while (!empty($db_files) && in_array($filename, $db_files));
//rename file
$newname = $dir . $filename;
$oldname = $dir . $file;
rename($oldname, $newname);
//add new filename to db_array here for checking on next loop
$db_files[] = $filename;
8
6
Quote
All times are EST. The time is now 12:56 pm.
- Normal Topic
- Sticky Topic
- Locked Topic
- New Post
- Sticky Topic W/ New Post
- Locked Topic W/ New Post
- View Anonymous Posts
- Able to post
- Filtered HTML Allowed
- Censored Content