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

Hi, it's me again. Your friendly neighbourhood voodoo doll. I'm having problems trying to write an upload script. I understand everything except the uploading of the files itself. here is a basic Psuedocode of the CGI document i want to run off of.
Start of the Form Checkboxes for $Color and $Pattern and $Installation Radio buttons for $Type 2 upload boxes one for $thumbnail and one for $screenres Restrictions on $thumbnail upload. ->must be under 50k, ->must end in td.jpg, ->must match the file name of $screenres once you take off tn.jpg Restrictions on $screenres upload. ->must be under 500k ->must end in sr.jpg, ->must match the file name of $thumbnail once you take off sc.jpg $thumbnail goes to the folder '/images/thumbnails/' $screenres goes to the folder '/images/screenres/' $name = $thumbnail minus tn.jpg or $screenres minus sc.jpg finnally a line is appended to the end of the db file '/cgi/scfrom.db' '$name|$color|$pattern $installation|$type|$thumbnail|$screenres|#Chec +k Needed Here#' pending that the two files match and exist, the script then completes +it's task once the form is submitted
I can do everything there, except i am too confused/frustrated/new to understand how to upload the files. Please Help.
----------------------------
Wiz, The VooDoo Doll
Head Master of 12:30 Productions
----------------------------

Edit Masem 2001-08-22 - Edited title from "Frustrated Headaches with Uploading Pictures"

Replies are listed 'Best First'.
Re: Frustrated Headaches with Uploading Pictures
by tachyon (Chancellor) on Aug 22, 2001 at 03:54 UTC

    If you Super Search for 'file upload' in the text you will be inundated with links. If you add my name 'tachyon' as the author you will find (amongst others) this link Re: File Upload - What Next? which contains an entire working upload script.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Frustrated Headaches with Uploading Pictures
by cLive ;-) (Prior) on Aug 22, 2001 at 07:54 UTC
    use CGI.pm. Read the man page, particulaly this and this.

    Should keep you going fo a while. Show us some code if you get stuck again..

    cLive ;-)

Re: Frustrated Headaches with Uploading Pictures
by John M. Dlugosz (Monsignor) on Aug 22, 2001 at 03:52 UTC
    Here's mine:

    (on the page)

    <FORM action="upload_action.asp" method="post" enctype="multipart/form +-data"> File on your machine to upload: <INPUT TYPE="file" NAME="file">
    (in the script)
    my $len= $Request->TotalBytes(); my $input= $Request->BinaryRead ($len); # parse the stuff my $stuff= parse_multipart ($input); undef $input; # recover memory ASAP my $filename= $stuff->{filename}{'#body'}; # ... make sure the filename is OK. tr[\\/:][$]; # could do more. # write it out. unless (open FILE, ">" . $Server->MapPath ("uploads/$filename")) { $Response->Write ("The filename \"" . $Server->HTMLEncode($fil +ename) . "\" could not be opened for writing. Hit the BACK button an +d edit the form, and try again."); exit; } binmode FILE; print FILE $stuff->{file}{'#body'}; my $size= length $stuff->{file}{'#body'}; delete $stuff->{file}; #recover the memory ASAP. close FILE; if (open FILE, ">" . $Server->MapPath ("uploads/$filename.comment" +)) { print FILE "Comment for \"$filename\": $stuff->{comment}{'#body +'}\n"; print FILE "Email: $stuff->{email}{'#body'}\n"; print FILE "Name: $stuff->{name}{'#body'}\n"; close FILE; } # return a list of parts. # each part is a hash containing the header information # The "filename" included in the Content-Disposition line is indexed a +s "#filename". # body is indexed as "#body". # everything else indexed as header name up to (not including) the ':' # index by the name found in the Content-Disposition line. sub parse_multipart { my $input= shift; my %result; #first determine the delimiter $input =~ m/^--(.*?)\r?\n/sg; my $delimiter= $1; print "Delimiter is: [$delimiter]\n"; print "pos: ", pos($input), "\n"; #than split (watch for trailing newline or "--"), include leading new +line in delimiter. while ($input =~ /\G(.*?)\r?\n--$delimiter(--|\r?\n)/sg) { my ($content,$endmark) = ($1, $2); #further parse each part. my %data; my ($header, $name); ($header, $data{'#body'})= split (/\r?\n\r?\n/, $content, 2); foreach my $line (split (/\r?\n/, $header)) { if ($line =~ /^Content-Disposition:/) { ($name) = $line =~ /\Wname="(.*?)"/; my ($filename) = $line =~ /filename="(.*?)"/; $data{'#filename'}= $filename if defined $filename; } else { my ($tag,$value)= split (/:\s/, $line, 2); $data{$tag}= $value; } } $result{$name}= \%data; last if $endmark eq '--'; # by definition, ignore anything after + this. } return \%result; }
Re: Uploading images with CGI.pm
by wiz (Scribe) on Aug 23, 2001 at 02:40 UTC
    Here is some sample code for the updating part (too bad i can't get it to work =^P
    #!/usr/bin/perl -w # formcgi.cgi use strict; use CGI; my $query = new CGI; print $query->header; print $query->start_html; print $query->start_multipart_form(-action=>"test2.cgi"); print $query->filefield(-name=>'uploaded_file', -default=>'starting value', -size=>50, -maxlength=>80); print $query->br; print $query->textfield('name','starting value',50,80); print $query->submit(-name=>'button_name', -value=>'value'); print $query->end_multipart_form; print $query->end_html;
    and file 2:
    #!/usr/bin/perl -w #test2.cgi use CGI; use strict; my $query = new CGI; print $query->header; print $query->start_html; my $filename = $query->param('uploaded_file'); my $name = $query->param('name'); my ($buffer, $bytesread); open (OUTFILE,">/images/screenres/$name.jpg") or die "This didn't open + the file $!"; while ($bytesread=read($filename,$buffer,1024)) {{ print OUTFILE $buffer; } } close OUTFILE; print $query->p("$name $filename"); print "<img src=\"/images/screenres/$name.jpg\">"; print $query->end_html;
    ----------------------------
    Wiz, The VooDoo Doll
    Head Master of 12:30 Productions
    ----------------------------
A reply falls below the community's threshold of quality. You may see it by logging in.