in reply to Changing file names as they are uploaded

How about:

my $ext = ""; if ($imagename =~ s/(\.jpe?g|\.gif|\.png)\z//i) { # updated $ext = $1; } $imagename =~ s/\W/_/g; # updated $imagename = $imagename.$ext; # updated
You do need to specify valid suffixes, but as you are only interested in images, it shouldn't be too much of a problem.

Update: Fixed some bugs pointed out by blakem. That'll teach me to test my code :-)

-- Joost downtime n. The period during which a system is error-free and immune from user input.

Replies are listed 'Best First'.
Re2: Changing file names as they are uploaded
by blakem (Monsignor) on Sep 19, 2002 at 12:32 UTC
    tr/\W/_/
    tr doesn't work that way... You'll have to stick with s/// if you want to use character classes. Also your logic will add a '.' on to the end of a file w/o an extension. i.e. "xyzfile" => "xyzfile."

    -Blake