Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

difficult error checking

by Anonymous Monk
on Jan 28, 2004 at 19:48 UTC ( [id://324757]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I don't know what to call this, but I'm confused! I have a form that uploads images (upload#). Since I have the option of uploading more than 1 file at a time, I pass the work to a subroutine with an increment on $num.

Everything works fine except the new error checking I tried to make. Since each image filename is stored into a hash, I need them to be different. I tried using a foreach(keys.. to check, and this DOES work...kind of.

The problem is, if upload1 and upload2 are unique, but upload3 isn't, images 1 and 2 will be sucessful and image 3 will die. Likewise, if image 1 is a duplicate and 2 and 3 are different, because image1 dies, the rest can't be successfull.

Can someone help me figure out a way to set it up to print "$filename was uploaded" for ALL images that are unique, even if the image before it was a duplicate?

An example:

___DATA___ dog.gif cat.gif bird.gif Upload form attempts: upload1 = bat.gif upload2 = dog.gif upload3 = gopher.gif Print out results: bat.gif was successfull dog.gif was a duplicate, not uploaded gopher.gif was successfull

SCRIPT:

if ( param('upload1') ) { my $num = 1; &dirty_work($num); } if ( param('upload2') ) { my $num = 2; &dirty_work($num); } if ( param('upload3') ) { my $num = 3; &dirty_work($num); } if ( param('upload4') ) { my $num = 4; &dirty_work($num); } sub dirty_work { my $num = shift; # take form data my $remotefile = param("upload$num"); # make new variable to prevent overwriting of form data my $filename = $remotefile; my $title = param("title$num"); $title =~ s/&/&\#38/g; my $comments = param("comments$num"); $comments =~ s/&/&\#38/g; # remove all directories in the file name path $filename =~ s/^.*[\\\/]//; my $localfile = "$uploaddir/$filename"; my $type = uploadInfo($remotefile)->{'Content-Type'}; unless ( $type eq 'image/pjpeg' || $type eq 'image/gif' || $type e +q 'image/bmp') { print "Wrong! This is not a supported file type."; exit; } ################################## # Check for duplicate filenames ################################## foreach (keys %upload) { my ($filename_check, $title_check, $comments_check, $w_check, $h_che +ck, $count_check) = split(/::/, $upload{$_}); if($filename eq "$filename_check") { print "<center><font color=red>Duplicate filename found: $filename +_check</font></center>"; exit; } } print "<font color=blue><center>Uploaded $filename</font></center>";

Replies are listed 'Best First'.
Re: difficult error checking
by Sol-Invictus (Scribe) on Jan 28, 2004 at 21:48 UTC
    use a hash to filter doubles
    #! perl -w use strict; my($file); my(%upload,%seen); %upload=(a=> 'dog', b=> 'cat', c=> 'llama', d => 'dog' ); for (keys %upload){ $file=$upload{$_}; if(defined $seen{$file}) { print "<center><font color=red>Duplicate filename found: $ +file</font></center>\n"; exit; } $seen{$file}=$file; print "<font color=blue><center>Uploaded $file</font></center>"; + }
    Instead of the exit call you could just make the filename more unique by tagging on a number (much like your OS does when you make try to make a file with a non unique name)

    #! perl -w use strict; my(%upload,%seen); my($counter,$file,$old); %upload=(a=> 'dog.gif', b=> 'cat.gif', c=> 'llama.gif', d => 'dog.gif' ); for (keys %upload){ $file=$upload{$_}; if(defined $seen{$file}) { $old=$file; while (defined $seen{$file}){ $file=$old; $counter++; $file=join('.',$file,$counter); } } $seen{$file}=$file; print "<font color=blue><center>Uploaded ",$file,"</font></cente +r>\n"; }
Re: difficult error checking
by fireartist (Chaplain) on Jan 29, 2004 at 09:47 UTC
    It may not be suitable for your application, but I normally assign my own filename to any uploaded file.
    Something like,
    sub new_filename { my $filename = shift; my $ext; my $newname; if ($filename =~ /.*(\.[a-zA-Z0-9]{2,4})$/) { $ext = $1; } else { return; } do { $newname = rand_string() . $ext; } while (-e $newname); return $newname; } sub rand_string { my @char = (0..9, 'a'..'z', 'A'..'Z'); my $string = ''; for (1 .. 15) { $string .= $char[rand 64]; } return $string; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://324757]
Approved by calin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-25 11:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found