problem with file upload script

Posted By: dubz45 ()
Posted On: 2004-May-07 21:19

Hi,
I created a form on my site that allows users to type in text data such as name, address, phone, etc and upload a picture. The problem I'm having is creating a script that will retrieve the text data along with the file upload and then send it to me via email with an image attachment. The file upload part is what's really getting me. I can get the text sent just no file. Any ideas. This is keeping me up all night.
My server uses Unix and the updated Perl software.

Here's my form and script:


<CENTER><FORM name='listing' action='/cgi-bin/listing.pl' method='POST' ENCTYPE='multipart/form-data'>
<TABLE border="0" cellpadding="2" cellspacing="2" >
<TR>
<TD>
Name:
</TD>
<TD>
<INPUT type='text' name='name' size='15' value=''>
</TD>
</TR>
<TR>
<TD>
Address:
</TD>
<TD>
<INPUT type='text' name='address' size='15' value=''>
</TD>
</TR>
<TR>
<TD>
City:
</TD>
<TD>
<INPUT type='text' name='city' size='15' value=''>
</TD>
</TR>
<TR>
<TD>
Zip:
</TD>
<TD>
<INPUT type='text' name='zip' size='10' value=''>
</TD>
</TR>
<TR>
<TD>
Telephone:
</TD>
<TD>
<INPUT type='text' name='telephone' size='10' value=''>
</TD>
</TR>
<TR>
<TD>
Email:
</TD>
<TD>
<INPUT type='text' name='email' size='15' value=''>
</TD>
</TR>
<TR>
<TD>
Asking Price:
</TD>
<TD>
<INPUT type='text' name='price' size='15' value=''>
</TD>
</TR>
<TR>
<TD>
Features:
</TD>
<TD>
<TEXTAREA name='features' rows='5' cols='25'></TEXTAREA>
</TD>
</TR>
<TR>
<TD>
Details:
</TD>
<TD>
<TEXTAREA name='details' rows='5' cols='25'></TEXTAREA>
</TD>
</TR>
<TR>
<TD>
Upload Picture:
</TD>
<TD>
<INPUT type='file' name='upload' size='15'>
</TD>
</TR>
<TR>
<TD>

</TD>
<TD>
<INPUT type='checkbox' name='check' value='' >
</TD>
</TR>
<TR>
<TD>

</TD>
<TD>
<INPUT type='submit' name='submit' value='Submit Property:'>
</TD>
</TR>
</TABLE>
</FORM></CENTER>


HERE'MY SCRIPT:


#!/usr/bin/perl
$mail_prog = '/usr/sbin/sendmail' ;
# This script was generated automatically by Perl Builder(tm): http://www.solutionsoft.com

# ***ENDAUTOGEN:HEADER*** Do NOT modify this line!! You may enter custom code after this line.



# ***AUTOGEN:INPUT*** Do NOT modify this line!! Do NOT enter custom code in this section.

&GetFormInput;

# The intermediate variables below make your script more readable
# but somewhat less efficient since they are not really necessary.
# If you do not want to use these variables, clear the
# Intermediate Variables checkbox in the Tools Options dialog box, CGI Wizard tab.

$name = $field{'name'} ;
$address = $field{'address'} ;
$city = $field{'city'} ;
$zip = $field{'zip'} ;
$telephone = $field{'telephone'} ;
$email = $field{'email'} ;
$price = $field{'price'} ;
$features = $field{'features'} ;
$details = $field{'details'} ;
$upload = $field{'upload'} ;
$check = $field{'check'} ;
$submit = $field{'submit'} ;

# ***ENDAUTOGEN:INPUT*** Do NOT modify this line!! You may enter custom code after this line.



# ***AUTOGEN:VALIDATE*** Do NOT modify this line!! Do NOT enter custom code in this section.

# ***ENDAUTOGEN:VALIDATE*** Do NOT modify this line!! You may enter custom code after this line.



# ***AUTOGEN:LOGFILE*** Do NOT modify this line!! Do NOT enter custom code in this section.

# ***ENDAUTOGEN:LOGFILE*** Do NOT modify this line!! You may enter custom code after this line.



# ***AUTOGEN:EMAIL*** Do NOT modify this line!! Do NOT enter custom code in this section.

$recip = "wmzlotoff@yahoo.com" ;
if (($recip =~ /(@.*@)(..)(@.)(.@)(^.)/) ($recip !~ /^.+@([?)[a-zA-Z0-9-.]+.([a-zA-Z0-9]+)(]?)$/)) {
print "Fatal Error - Invalid email" ;
exit 0; }

open (MAIL, "$mail_prog -t"wink;
print MAIL "To: $recipn";
print MAIL "Reply-to: webmaster@buyahome-phoenix.comn";
print MAIL "From: webmaster@buyahome-phoenix.comn";
print MAIL "Subject: add listingn";
print MAIL "nn";
print MAIL "Name:".$name."n" ;
print MAIL "Address:".$address."n" ;
print MAIL "City:".$city."n" ;
print MAIL "Zip:".$zip."n" ;
print MAIL "Telephone:".$telephone."n" ;
print MAIL "Email:".$email."n" ;
print MAIL "Price:".$price."n" ;
print MAIL "Features:".$features."n" ;
print MAIL "Details:".$details."n" ;
print MAIL "Upload:".$upload."n" ;
print MAIL "nn";
close (MAIL);

# ***ENDAUTOGEN:EMAIL*** Do NOT modify this line!! You may enter custom code after this line.



# ***AUTOGEN:HTML*** Do NOT modify this line!! Do NOT enter custom code in this section.
print "Location: http://www.buyahome-phoenix.com/thankyou.htmlnURI: http://www.buyahome-phoenix.com/thankyou.htmlnn" ;

# ***ENDAUTOGEN:HTML*** Do NOT modify this line!! You may enter custom code after this line.



# ***AUTOGEN:ERRPRINT*** Do NOT modify this line!! Do NOT enter custom code in this section.

# ***ENDAUTOGEN:ERRPRINT*** Do NOT modify this line!! You may enter custom code after this line.



# ***AUTOGEN:PARSE*** Do NOT modify this line!! Do NOT enter custom code in this section.
sub GetFormInput {

(*fval) = @_ if @_ ;

local ($buf);
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN,$buf,$ENV{'CONTENT_LENGTH'});
}
else {
$buf=$ENV{'QUERY_STRING'};
}
if ($buf eq ""wink {
return 0 ;
}
else {
@fval=split(/&/,$buf);
foreach $i (0 .. $#fval){
($name,$val)=split (/=/,$fval[$i],2);
$val=~tr/+/ /;
$val=~ s/%(..)/pack("c",hex($1))/ge;
$name=~tr/+/ /;
$name=~ s/%(..)/pack("c",hex($1))/ge;

if (!defined($field{$name})) {
$field{$name}=$val;
}
else {
$field{$name} .= ",$val";

#if you want multi-selects to goto into an array change to:
#$field{$name} .= "$val";
}


}
}
return 1;
}


# ***ENDAUTOGEN:PARSE*** Do NOT modify this line!! You may enter custom code after this line.

#!/usr/bin/perl
#-----------------------------------------------------------
#-----
#----- Forms To Go v2.1.2 by Bebosoft
#-----
#----- http://www.bebosoft.com/
#-----
#-----------------------------------------------------------
$mailprog = '/usr/sbin/sendmail';

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);

foreach $pair (@pairs){
($name, $value) = split(/=/, $pair);

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$FORM{$name} = $value;
}

$name = $FORM{'name'};
$address = $FORM{'address'};
$city = $FORM{'city'};
$zip = $FORM{'zip'};
$telephone = $FORM{'telephone'};
$email = $FORM{'email'};
$price = $FORM{'price'};
$features = $FORM{'features'};
$details = $FORM{'details'};
$upload = $FORM{'upload'};
$check = $FORM{'check'};
$submit = $FORM{'submit'};


# Redirect user to FAIL page

if ($validationfailed == 1)
{


print "Location: http://www.buyahome-phoenix.com/error.htmlnn";
exit;

}

# Email Body

$body_form_email = "name: $namen"
. "address: $addressn"
. "city: $cityn"
. "zip: $zipn"
. "telephone: $telephonen"
. "email: $emailn"
. "price: $pricen"
. "features: $featuresn"
. "details: $detailsn"
. "upload: $uploadn"
. "check: $checkn"
. "submit: $submitn"
. "n"
. "";

# Send the email to the form owner

open(MAIL,"$mailprog"wink;
print MAIL "To: Wes Zlotoff <wmzlotoff@yahoo.com>n";
print MAIL "From: $addressn";
print MAIL "Subject: listingnn";
print MAIL $body_form_email;
close(MAIL);
# Redirect user to OK page

print "Location: http://www.buyahome-phoenix.com/thankyou.htmlnn";
exit;


Posted By: Prowler (Staff)
Posted On: 2004-May-08 11:19

It is far better to store the form contents to a directory from which you can patch together a mail at specified time to be despatched to you. This way you have a complete record of all contents sent through the form page.

The following snippet of code will do the job of saving the uploaded file to a directory in your server.
---------------- CODE -------------
if(open(OUTFILE, ">$dir/$filename"wink) {
while (my $bytesread = read($file, my $buffer, 1024)) {
print OUTFILE $buffer;
}

--------------END OF CODE -------------
Where:
$dir is the directory with write permission for the script/server. And
$filename is the name of the file for upload.

Whilst at it, make sure that you define a maximum size for the upload or someone will dump a huge file to bring your server to its knees.





Posted By: Ron C ()
Posted On: 2004-May-08 18:31

The script you're using is not designed to parse out multipart formdata (i.e., uploaded files). Doing so is MUCH more complex than the simple GET/POST algorithm in your GetFormInput subroutine.

I suggest you look for something at any of the many script depositories that is specifically designed for uploading files, rather than just simple forms processing. You'll discover, I think, that such scripts either use Libraries for processing the input or include substantially more logic than you have in this script.