[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

5.8

Form mailer/File upload CGI

Asked by jamaica in CGI Scripting, Server Side Includes (SSI)

I did a form mailer file upload cgi but having a bit of problem with setting required fields.

To put things in layman terms, the form work similar to regular form mailer, user submits their info e.g., name, address, phone, email -- all these fields SHOULD be required fields (must be filled in) and there's a file upload field for the user to select a file on his local drive and send to a server path.  

The problem I'm having -- the file upload field is set as a required field, therefore, if the user does not select a file to upload, he will get a warning message saying that a file upload is needed.  I do not want it so, this file upload field should NOT be a required field.  The other fields, such as name, address, phone, email currently are not required fields but I need these fields TO BE required fields. Example, if the user does not fill out the phone or email field, he should get a warning page saying something like "Error-Missing Fields" and a list of the required fields that weren't filled in.

So right now, the form is working in the opposite way where the required fields are concerned.  That is, the file upload is currently a required field -> FILE UPLOAD MUST NOT BE A REQUIRED FIELD.  Fields such as name, email, phone, address, currently are not required fields -> THESE INPUT FIELDS MUST BE REQUIRED FIELDS.

I'm asking someone to look over this cgi script. I've done alot of work to it -- solving the error 500; getting fields to print out on screen, setting up the confirmation receipt section, etc., but I just cannot figure out how to fix in the required fields.

Your help is major appreciated!

------cgi script---

#!/usr/local/perl/bin/perl

$imagedir = "/usr/local/opt/me/html/upload";


# Full path on your server to log file

$log = "/usr/local/opt/me/html/bin/up.log";


# set to the number of files to upload +1
# (e.g. for 10 files $numfiles = "11")

$numfiles = "5";


# refers

@referers = ('my.work.com', 'work.my.com', 'me.atwork.com', '123.456.789.10');

# Retrieve Date
&get_date;

## Do you wish to allow all file types?  yes/no (no capital letters)

$allowall = "yes";

## If $allowall = "no" enter file extensions below (no capital letters)

@filetypes = ();


$EMail = "yes";                                    

$sendmail="/usr/lib/sendmail";                   

$ConfirmToEmail = "me\@at.work.com";       

$SubjectEmail="Test Form results";       

############ Results Page Background and Colour Options ############

$bgcolor = "#FFFFFF";

$text = "#000000";

$link = "#0000FF";

$vlink = "#800080";

$background = "";


############ Results Page Text Options ############

$html_title="Whatever";

$html_header="Whatever 1";

$html_header2="Whatever 2";

$message = "Thank you for submitting your request.  This is your confirmation of receipt.";


############ Results Page Link Options ############

$link1 = "http://my.work.com/services/requests/1.html";

$text_link1 = "Whatever 1";

$link2 = "http://my.work.com/services/requests/2.html";

$text_link2 = "Whatever 2";

$link3 = "http://my.work.com/services/requests/3.html";

$text_link3 = "Whatever 3";

$link4 = "http://my.work.com";

$text_link4 = "Home";

################################################
## SANDI THIS IS THE IMPORTANT SECTION        ##
################################################

$body = "<body BACKGROUND=\"$background\" bgcolor=\"$bgcolor\" text=\"$text\" link=\"$link\" vlink=\"$vlink\">";

$html_header3="Completed and submitted by:";

#$EmailMessage = "New files have been uploaded!";       #email message#

$numfiles++;

# Check Referring URL
&check_url();

use CGI;

$onnum = 1;
while ($onnum != $numfiles) {
my $req = new CGI;
if ($req->param("FILE1") eq "") { &error_nofile; }
my $file = $req->param("FILE$onnum");
if ($file ne "") {
my $fileName = $file;
$fileName =~ s!^.*(\\|\/)!!;
$newmain = $fileName;

$REQUESTERS_NAME = $req->param("REQUESTERS_NAME");
$EMAIL = $req->param("EMAIL");
$PHONE = $req->param("PHONE");
$WORK_ADDRESS = $req->param("WORK_ADDRESS");


### Validate File Types ###

$filegood = "yes";
if ($allowall ne "yes") { &check_filetypes; }


### Do the Work ###

if ($filegood eq "yes") {

      open (OUTFILE, ">$imagedir/$fileName");

      #print "<tr><td>File $onnum: $fileName</td></tr>";
      #if ($EMail eq "yes") { print MAIL "$newmain\n\n"; }
      #if ($log ne "") { push(@filelist,"$fileName"); }
      push(@filelist,"$fileName");
      $temp = "File $onnum: $fileName";
      push(@filelist2,"$temp");

      while (my $bytesread = read($file, my $buffer, 1024)) {
            print OUTFILE $buffer;
            }
      close (OUTFILE);
      }
}
$onnum++;
}


if ($EMail eq "yes") { &send_mail; }
if ($log ne "") { &log_3; }
&print_html;

exit;

## Start of SubRoutines ##

###############################################################
sub get_date {

    # Define arrays for the day of the week and month of the year.           #
    @days   = ('Sunday','Monday','Tuesday','Wednesday',
               'Thursday','Friday','Saturday');
    @months = ('January','February','March','April','May','June','July',
               'August','September','October','November','December');

    # Get the current time and format the hour, minutes and seconds.  Add    #
    # 1900 to the year to get the full 4 digit year.                         #
    ($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
    $time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);
    $year += 1900;

    # Format the date.                                                       #
    $date = "$days[$wday], $months[$mon] $mday, $year at $time";

}

###############################################################
sub send_mail {

            #$upload2 = join(", ",@filelist);

         # Open and write Confirmation Email

             open(MAIL,"|$sendmail -t");

          print MAIL "To: $ConfirmToEmail\n";
          print MAIL "From: $ConfirmToEmail\n";
          print MAIL "Subject: $SubjectEmail\n\n\n" ;


          print MAIL "Below is the result of a Create a New Service Request. It was submitted by\n";
          print MAIL "$REQUESTERS_NAME $EMAIL on $date\n";
          print MAIL "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n";
        
      print MAIL "\n";

            foreach $i (@filelist2) {print MAIL "$i\n\n";}

            print MAIL "REQUESTER'S NAME: $REQUESTERS_NAME\n";
            print MAIL "EMAIL: $EMAIL\n";
               print MAIL "REQUESTER'S PHONE CONTACT: $PHONE\n";
               print MAIL "ADDRESS: $WORK_ADDRESS\n";
               print MAIL "\n\n\n";
               close (MAIL);

       }


###############################################################
sub redirect {

      print "Location:$donepage\n\n";

      #exit;

}

###############################################################
sub print_html {

      print "Content-type:text/html\n\n";
      print "<HTML><HEAD><TITLE>$html_title</TITLE></HEAD>\n";
      print "$body\n";
      print "<center><BR><h2>$html_header</h2></center>\n";
      print "<center><BR><h4>$html_header2</h4></center>\n";
      print "<center><table border=0 cellpadding=0 cellspacing=0 width=>\n";
      print "<tr><td align=left>\n";
      print "<center><table border=2 cellpadding=10 cellspacing=2 width=>\n";

      foreach $i (@filelist2) {print "<tr><td>$i</td></tr>\n";}

      print "</table></center><BR>\n";
      print "<center><BR><h4>$html_header3</h4></center>\n";

      print "<HR><table width=100% border=0 cellpadding=2 cellspacing=2>\n";
      print "<tr><td valign=top>\n";
      print "REQUESTER'S NAME:</td><td>$REQUESTERS_NAME";
      print "</td></tr></table>\n";

      print "<table width=100% border=0 cellpadding=10 cellspacing=2>\n";
      print "<tr><td valign=top>\n";
      print "REQUESTER'S EMAIL:</td><td>$EMAIL";
      print "</td></tr></table>\n";
      print "</td></tr></table>\n";

      print "<table width=100% border=0 cellpadding=2 cellspacing=2>\n";
      print "<tr><td valign=top>\n";
      print "REQUESTER'S PHONE CONTACT:</td></tr><tr><td>$PHONE";
      print "</td></tr></table>\n";

      print "<table width=100% border=0 cellpadding=2 cellspacing=2>\n";
      print "<tr><td valign=top>\n";
      print "ADDRESS:</td></tr><tr><td>$WORK_ADDRESS";
      print "</td></tr></table>\n";

      print "<BR><BR><HR width = \"100%\">\n";
      print "<h4><tt>$message</tt></h4>\n";
      print "<BR><BR>\n";

      print "<a href=$link1><tt>$text_link1</a></tt>; &nbsp;&nbsp;&nbsp\n";
      print "<a href=$link2><tt>$text_link2</a></tt>; &nbsp;&nbsp;&nbsp;\n";
      print "<a href=$link3><tt>$text_link3</a></tt>; &nbsp;&nbsp;&nbsp;\n";
      print "<a href=$link4><tt>$text_link4</a></tt>; \n";
      print "<BR><BR>\n";

      print "<BR><BR><HR width = \"100%\">\n";

      print "<h6><tt>Global warming.</tt></h6>\n\n";
      
      print "<BR></center></body></html>";

      #exit;
}


####### Check to see if another site is trying to use the program ########
sub check_url {

    # Localize the check_referer flag which determines if user is valid.     #
    local($check_referer) = 0;

    # If a referring URL was specified, for each valid referer, make sure    #
    # that a valid referring URL was passed to FormMail.                     #

    if ($ENV{'HTTP_REFERER'}) {
        foreach $referer (@referers) {
            if ($ENV{'HTTP_REFERER'} =~ m|https?://([^/]*)$referer|i) {
                $check_referer = 1;
                last;
            }
        }
    }
    else {
        $check_referer = 1;
    }

    # If the HTTP_REFERER was invalid, send back an error.                   #
    if ($check_referer != 1) { &error('bad_referer') }
}


###############################################################
sub error_filetype {

      print "Content-type:text/html\n\n";
      print "<HTML><HEAD><TITLE>Invalid File Type</TITLE></HEAD>$body\n";
      print "<BR><BR><center>\n";
      print "<B>You tried to upload an invalid file type ( $newmain )!</B>";
      print "<BR><BR>\n\n";
      $filetypes2 = join(", ",@filetypes);
      print "Files must end with: $filetypes2";
      #if ($allowzip eq "yes") { print ".zip, "; }
      print "<BR><BR>\n\n";
      print "Please check the file and try again!";
      print "<BR><BR>\n";
      print "</center></body></html>";

      exit;
}


####### Error Bad Referer #######
sub ref_error {

      print "Content-type:text/html\n\n";
      print "<HTML><HEAD><TITLE>Referer Error</TITLE></HEAD>$body\n";
      print "<BR><BR><CENTER><font color=red><h2>You do not have permision to use this script!</h2></font>\n";
      print "</CENTER><BR><BR>";
      print "</body></html>";

        exit;
      }


####### Error No File #######
sub error_nofile {

      print "Content-type:text/html\n\n";
      print "<HTML><HEAD><TITLE>No File</TITLE></HEAD>$body\n";
      print "<BR><BR><CENTER><font color=red><h2>You haven't selected a file to upload!</h2></font>\n";
      print "</CENTER><BR><BR>";
      print "</body></html>";

        exit;
      }


####### Check File Types #######
sub check_filetypes {

      $imageonly = "yes";
      if ($imageonly eq "yes"){
            $filegood = "no";
      foreach $i (@filetypes){
            if (lc(substr($newmain,length($newmain) - 4,4)) eq $i)
                  { $filegood = "yes"; }
            }
      if ($filegood eq "no") { &error_filetype; }
      }
}



####### Log 3 #######
sub log_3 {

      $upload = join(", ",@filelist);

      ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
      if (length ($min) eq 1) {$min= '0'.$min;}
      if ($year < 100) {$year = 2000 + $year;}
      else {$year  = 1900 + $year;}
      $mon++;
      $date="$mon/$mday/$year, $hour:$min";


      read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
      # open(LOG,">>$log") or &error_nofile;
      open(LOG,">>$log") || die $!;
      print LOG "$date\n";
      print LOG "Form submitted and file(s) uploaded by: $REQUESTERS_NAME <$EMAIL>\n";
      print LOG "Phone Contact: $PHONE\n";
      print LOG "Address: $WORK_ADDRESS\n";
      print LOG "FILES: $upload\n";
      print LOG "--------------------------------------------------------------------\n";
      close(LOG);

}
[+][-]05/16/01 01:44 AM, ID: 6085413Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05/16/01 07:59 AM, ID: 6086758Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05/17/01 02:08 AM, ID: 6089982Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05/18/01 10:49 AM, ID: 6096729Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05/18/01 11:48 AM, ID: 6096951Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05/18/01 01:47 PM, ID: 6097385Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05/18/01 03:20 PM, ID: 6097639Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05/18/01 10:33 PM, ID: 6098555Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05/19/01 11:10 AM, ID: 6099810Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05/19/01 02:26 PM, ID: 6100036Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05/19/01 10:28 PM, ID: 6100477Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: CGI Scripting, Server Side Includes (SSI)
Sign Up Now!
Solution Provided By: blago
Participating Experts: 1
Solution Grade: B
 
[+][-]05/20/01 06:04 PM, ID: 6101777Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05/23/01 01:12 AM, ID: 6111141Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05/23/01 11:49 AM, ID: 6113069Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05/31/01 09:12 AM, ID: 6142255Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89