in reply to Re^6: Renaming an image file
in thread Renaming an image file

Sorry, I don't know much about cgi, but it still looks to me that you are md5'ing the filename and not the contents of the file.

Replies are listed 'Best First'.
Re^8: Renaming an image file
by Anonymous Monk on Nov 28, 2010 at 18:44 UTC
    OK I will add another string of numbers right after the MD5 if this file already exists:
    if ( -e "$path/$filename" ) { my $unique = unique(); $filename=~s/^([^_]+_)(.*)(\.\w{3})\z/$1.$2.$unique.$3/eg; rename( "$path/$filename", "$path/$filename"); unlink($filename); } } sub unique { my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = loc +altime(time); $mon++; $year = $year + 1900; my $timestamp = sprintf "%4d%02d%02d%02d%02d%02d", $year,$mon,$mday,$h +our,$min,$sec; my $rand = (rand 10000); $rand = int($rand); my $unique = "$timestamp$rand"; }

      Well, okay. But all you've done is move the goal posts a little.

      Webservers are inherently multitasking, so adding all those digits of date and time is pretty useless as there could be many uploads completing within each given second.

      And depending upon the quality of your rand() function, the rand(10000) bit does almost nothing:

      perl -E" $t=time; ++$h{ rand(10000) } while time()==$t; my $n=0; $_ > 1 and $n+=$_ for values %h; say $n, ' duplicates in 1 second'; " 13793 duplicates in 1 second perl -E"$t=time; ++$h{ rand(10000) } while time()==$t; my $n=0; $_ > 1 + and $n+=$_ for values %h; say $n, ' duplicates in 1 second';" 400896 duplicates in 1 second >perl -E"$t=time; ++$h{ rand(10000) } while time()==$t; my $n=0; $_ > +1 and $n+=$_ for values %h; say $n, ' duplicates in 1 second';" 504998 duplicates in 1 second

      Even with what is probably the best PRNG available, you can still generate duplicates:

      c:\test>perl -MMath::Random::MT=rand -E"$t=time; 1 while time==$t; ++$ +h{ rand(10000) } while time()==$t+1; my $n=0; $_ > 1 and $n+=$_ for v +alues %h; say $n, ' duplicates in 1 second';" 4 duplicates in 1 second c:\test>perl -MMath::Random::MT=rand -E"$t=time; 1 while time==$t; ++$ +h{ rand(10000) } while time()==$t+1; my $n=0; $_ > 1 and $n+=$_ for v +alues %h; say $n, ' duplicates in 1 second';" 4 duplicates in 1 second >perl -MMath::Random::MT=rand -E" $t=time; 1 while time==$t; ++$h{ rand(10000) } while time()==$t+1; my $n=0; $_ > 1 and $n+=$_ for values %h; say $n, ' duplicates in 1 second'; " 6 duplicates in 1 second

      It takes only a few milliseconds to read and md5 the contents of a medium sized image file.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.