I need to allow users to upload an image via form. I based my script upon a template that someon ein my company had used in the past, but the upload part is not working. Can anyone see anything that might be causing the problem? Is it the file upload path? Thanks!
#!/usr/bin/perl -w
#NOTE: Don't forget to modify above path to Perl for the current server!!!
# Global Variables
$mail_prog = "/usr/lib/sendmail";
use CGI::Carp qw(fatalsToBrowser);
# ***********************
# **** submit.pl ****
# ***********************
# This Perl script upload files for Knight PSE 3e site
# Get library and parse client input
use CGI;
$request = new CGI;
print "Content-Type: text/html\n\n";
# Define array of possible file extentions
@Extention = (
"jpg", "tif");
# Define Date and Time
$date_time = localtime;
# Define max file size
$max_size = 500_000;
# Get values from the form
$name = $request->param('name');
$school = $request->param('school');
$email = $request->param('email');
$mailingaddress = $request->param('mailingaddress');
$imagefile = $request->param('imagefile');
$filename = $imagefile;
$legal = $request->param('legal');
# Check for empty parameters
if (!defined($name) || $name eq "") {$name = "EMPTY";}
if (!defined($school) || $school eq "") {$school = "EMPTY";}
if (!defined($email) || $email eq "") {$email = "EMPTY";}
if (!defined($mailingaddress) || $mailingaddress eq "") {$mailingaddress = "EMPTY";}
if (!defined($imagefile) || $imagefile eq "") {$imagefile = "EMPTY";}
if (!defined($legal) || $legal ne "Agree") {$legal = "EMPTY";}
my $file_name = $filename;
$file_name =~ s/.*\\//;
$full_file_name = $file_name;
print ("$file_name");
# Error Check
CheckFields();
#******************************************
# Upload the file
#******************************************
# Go to our UPLOAD directory
chdir("../../knightpse3einfo/photos/");
$fileupload = "$full_file_name";
open(UPLOAD, ">$fileupload") or die $!;
while(<$imagefile>) {
print UPLOAD $_;
}
close(UPLOAD);
chmod(0775, $fileupload);
open (MAIL,"|$mail_prog -t") || die "Call to sendmail failed";
print MAIL << "!!EOM!!";
To: emily.wieja\@pearson.com
From: $email
CC: webmaster\@aw.com
Subject: Knight PSE 3e - iPad drawing submission
Name: $name
School: $school
E-mail: $email
Address: $mailingaddress
Picture has been successfully uploaded to the server: http://www.pearsonhighered.com/knightpse3einfo/photos/$fileupload
I have read the following and I $legal
!!EOM!!
close(MAIL);
# Start HTML output
#******************************************
# Thank you page
#******************************************
print <<"!!EOX!!";
<script type="text/javascript" language="JavaScript" >
document.location = "http://www.pearsonhighered.com/knightpse3einfo/detail/thanks/index.html";
</script>
!!EOX!!
exit(1);
sub InArray
{
local *array = $_[0];
local $element = $_[1];
foreach my $s (@array)
{
if ($s eq $element) {return 1};
}
return 0;
}
sub PrintArray
{
my @array = @_;
my $printout = "";
foreach my $s (@array)
{
if ($printout ne "")
{
$printout = $printout . ",";
}
$printout = $printout . $s;
}
return $printout;
}
sub CheckFields()
{
my $error_msg = "";
# Check that's all the required fiels
if ($name eq "EMPTY" || $school eq "EMPTY" || $email eq "EMPTY" || $mailingaddress eq "EMPTY" || $imagefile eq "EMPTY" || $legal eq "EMPTY")
{
$error_msg = "Some required fields are missing. Please fill out all the required field and submit the form again.";
}
# Make sure that the file with the same name doesn't exist in the folder
elsif (-e "../../knightpse3einfo/photos/$full_file_name")
{
$error_msg = "You are trying to upload the file <b>$full_file_name</b>.<br><br>The file with specified name already exists in the folder.</p>"
}
# Checking file extention
elsif (!($file_name =~ /^(.*)\.(\w*)$/) || !InArray(\@Extention, $2))
{
my $extention_print = PrintArray(@Extention);
$error_msg = "The file extension you specified <b>$2</b> is not supported. It should be one from the following list: <b>$extention_print</b>";
}
if( $error_msg ne "" )
{
# Send error message to client
print <<"!!EOX!!";
<script type="text/javascript" language="JavaScript" >
document.location = "http://www.pearsonhighered.com/knightpse3einfo/detail/error/index.html";
</script>
!!EOX!!
exit;
} # End if( $error_msg = "" )
} # End CheckFields()