Welcome to Geeklog, Anonymous Wednesday, November 27 2024 @ 03:33 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?
 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.
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
 Quote

Status: Banned

machinari

Forum User
Full Member
Registered: 03/22/04
Posts: 1512
Thanks Blaine. that looks good.
 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:
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;
 
of course that all makes more sense if you see it in a loop. :wink:
 Quote

All times are EST. The time is now 03:33 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