CGI file upload script problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • owz2008
    New Member
    • Jun 2008
    • 14

    CGI file upload script problems

    This has probably been covered before but could not find a similar thread.

    Basically I have created a form which can be viewed at www.icomworks.c o.uk/canvaspayform.h tml

    I want to submit the form along with the file so that they are both placed on my server... I have created a folder on my server in my public_html called myscripts and have saved my upload.cgi script into that folder. my form points to that script but when I fill in the form and submit the form I get the following error message.

    Internal Server Error

    The server encountered an internal error or misconfiguratio n and was unable to complete your request.
    Please contact the server administrator, webmaster@icomw orks.co.uk and inform them of the time the error occurred, and anything you might have done that may have caused the error.

    More information about this error may be available in the server error log.

    I have given the folder which the script is in the right permissions (755) and I have the upload path "i think" pointing to the right folders..

    here are the code for the form and the script

    Code:
    <form name="contactdet" action="myscripts/upload.cgi"method="post" onSubmit="return validate_form ( );" enctype="multipart/form-data">
    						  <p>First name: <input type="text" name="name1" id="name1" size="20"/></p>
    						  <p>Last name: <input type="text" name="name2" id="name2" size="20"/></p>
    						  <p>Email address:<input type="text" name="email" id="email_address" size="20" /></p>
    						  <p>Contact number:<input type="text" name="phone" id="phone" size="20"/></p>
    						  <p>House number: <input type="text" name="housenum" id="housenum"/></p>
    						  <p>Address 1:<input type="text" name="add1" id="add1" /></p>
    						  <p>Address 2:<input type="text" name="add2" id="add2" /></p>
    						  <p>Town:<input type="text" name="town" id="town" class="town"/></p>
    						  <p>Post code:<input type="text" name="pcode1" id="pcode" size="10" class="pcode1"/></p>
    				
    				 	  <p>Picture upload:<input type="file" name="photo" /></p>	  
    						  <p><input type="submit" name="submit" value="Upload Canvas Image" /></p></form>

    and this is the cgi script I created:
    Code:
    #!/usr/bin/perl -wT 

use strict; 
use CGI; 
use CGI::Carp qw ( fatalsToBrowser ); 
use File::Basename; 


    $CGI::POST_MAX = 1024 * 5000; 

    my $safe_filename_characters = "a-zA-Z0-9_.-"; 

    my $upload_dir ="public_html/canvasimages"; 


    my $query = new CGI; 

    my $filename = $query->param("photo"); 

    my $email_address = $query->param("email_address"); 

    
    
if ( !$filename ) 
{ 
*print $query->header ( ); 

    
    *print "There was a problem uploading your photo (try a smaller file)."; 
*exit; 
} 

    
    
my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' ); 
$filename = $name . $extension; 
$filename =~ tr/ /_/; 
$filename =~ s/[^$safe_filename_characters]//g; 

if ( $filename =~ /^([$safe_filename_characters]+)$/ ) 
{ 
*$filename = $1; 
} 
else 
{ 
*die "Filename contains invalid characters"; 
} 


    
    my $upload_filehandle = $query->upload("photo"); 

open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!"; 
binmode UPLOADFILE; 


    
    while ( <$upload_filehandle> ) 
{ 
*print UPLOADFILE; 
} 

close UPLOADFILE; 

print $query->header ( ); 
print <<END_HTML; 
    
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> 

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
*<head> 
* *<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
* *
    <title>Thanks!</title> 
* *
    <style type="text/css"> 
* * *img {border: none;} 
* *</style> 
*</head> 
*
    
    <body> 
* *
    <p>Thanks for uploading your photo!</p> 
* *
    <p>Your email address: $email_address</p> 
* *
    <p>Your photo:</p> 
* *
    <p><img src="/upload/$filename" alt="Photo" /></p> 

    *</body> 

    </html> 
END_HTML
    i put this script in text edit and saved it. I then changed the file name to .cgi is this correct?

    Im using an apache server and I use a mac to make the forms and script

    I'm sorry the spacing is not right when i pasted the code into the boxes they displayed all together... i've tried to break it up a bit to make it more understandable. .

    thnaks for taking the time to read this

    Owain
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Your web host should have some instuctions on how to run cgi scripts on their servers. Since we do not know how the host is configured it is hard to make anything but very general suggestions:

    upload the script in ASCII (text) mode
    set permissions (chmod) to 755

    generally you upload cgi scripts into the cgi-bin, not into a folder you create in your public_html folder, although that might be possible but you have to ask your web host.

    Comment

    • owz2008
      New Member
      • Jun 2008
      • 14

      #3
      Thanks for the reply Kevin,

      I have set the permissions to 755 and I know what your saying about putting the script into the cgi-bin but the server admin told me to put it in a seperate folder in the public_html because pipex who the servers are with do not like them being stored there..

      Can you see any errors in my script?

      I want the images once uploaded to go into a folder called canvasimages in the public_html folder. is the path right in my script?

      Also where will the form contact details go when the image and the form is submitted?

      I will have another chat with the server administrator tomorrow to see what they say..

      thanks again

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        All those "?" in the code you ppsted make me think that your mac editor is throwing in lots of extra data into the file that should not be there. Perl scripts must be saved in plain text (ASCII) formatted files. Don't use any word processing formats. But I don't know crap about mac computers so can't really suggest what you should do.

        Comment

        • owz2008
          New Member
          • Jun 2008
          • 14

          #5
          Hello again Kevin, I typed the perl script into a windows notepad document on my pc and then saved that as a cgi file. I then uploaded that to the server.

          Now when i submit the form i get the following error...

          Software error:

          Can't find string terminator "END_HTML" anywhere before EOF at upload.cgi line 50.

          the line their referring to has the following code: print <<END HTML;

          any ideas?

          Comment

          • owz2008
            New Member
            • Jun 2008
            • 14

            #6
            Originally posted by owz2008
            Hello again Kevin, I typed the perl script into a windows notepad document on my pc and then saved that as a cgi file. I then uploaded that to the server.

            Now when i submit the form i get the following error...

            Software error:

            Can't find string terminator "END_HTML" anywhere before EOF at upload.cgi line 50.

            the line their referring to has the following code: print <<END HTML;

            any ideas?
            here is the script again with better spacing..

            Code:
            #!/usr/bin/perl -wT 
            
            use strict; 
            use CGI; 
            use CGI::Carp qw ( fatalsToBrowser ); 
            use File::Basename; 
            
            $CGI::POST_MAX = 1024 * 5000; 
            my $safe_filename_characters = "a-zA-Z0-9_.-"; 
            my $upload_dir = "/public_html/canvasimages"; 
            
            my $query = new CGI; 
            my $filename = $query->param("photo"); 
            my $email_address = $query->param("email_address"); 
            
            if ( !$filename ) 
            { 
             print $query->header ( ); 
             print "There was a problem uploading your photo (try a smaller file)."; 
             exit; 
            } 
            
            my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' ); 
            $filename = $name . $extension; 
            $filename =~ tr/ /_/; 
            $filename =~ s/[^$safe_filename_characters]//g; 
            
            if ( $filename =~ /^([$safe_filename_characters]+)$/ ) 
            { 
             $filename = $1; 
            } 
            else 
            { 
             die "Filename contains invalid characters"; 
            } 
            
            my $upload_filehandle = $query->upload("photo"); 
            
            open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!"; 
            binmode UPLOADFILE; 
            
            while ( <$upload_filehandle> ) 
            { 
             print UPLOADFILE; 
            } 
            
            close UPLOADFILE; 
            
            print $query->header ( ); 
            print <<END_HTML; 
            
            
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> 
            <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
             <head> 
               <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
               <title>Thanks!</title> 
               <style type="text/css"> 
                 img {border: none;} 
               </style> 
             </head> 
             <body> 
               <p>Thanks for uploading your photo!</p> 
               <p>Your email address: $email_address</p> 
               <p>Your photo:</p> 
               <p><img src="/upload/$filename" alt="Photo" /></p> 
             </body> 
            </html> 
            END_HTML

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              Replace this:

              Code:
              print <<END_HTML; 
              
              
              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> 
              <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
               <head> 
                 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
                 <title>Thanks!</title> 
                 <style type="text/css"> 
                   img {border: none;} 
                 </style> 
               </head> 
               <body> 
                 <p>Thanks for uploading your photo!</p> 
                 <p>Your email address: $email_address</p> 
                 <p>Your photo:</p> 
                 <p><img src="/upload/$filename" alt="Photo" /></p> 
               </body> 
              </html> 
              END_HTML
              replace with:

              Code:
              print qq{ 
              
              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> 
              <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
               <head> 
                 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
                 <title>Thanks!</title> 
                 <style type="text/css"> 
                   img {border: none;} 
                 </style> 
               </head> 
               <body> 
                 <p>Thanks for uploading your photo!</p> 
                 <p>Your email address: $email_address</p> 
                 <p>Your photo:</p> 
                 <p><img src="/upload/$filename" alt="Photo" /></p> 
               </body> 
              </html> 
              };
              Then you don't have to worry about your HERE docs being terminated properly.

              Comment

              • owz2008
                New Member
                • Jun 2008
                • 14

                #8
                Thanks Kevin will try that,

                What if I want to place a link in the perl script which links to a page for example www.icomworks.c o.uk/canvaspayform2. html instead of putting the actual html within the script.. how is this done?


                thanks again, I appreciate the help..

                Owain

                Comment

                • owz2008
                  New Member
                  • Jun 2008
                  • 14

                  #9
                  I tried replacing the code and I know get a different error?

                  Internal Server Error

                  The server encountered an internal error or misconfiguratio n and was unable to complete your request.
                  Please contact the server administrator, webmaster@icomw orks.co.uk and inform them of the time the error occurred, and anything you might have done that may have caused the error.

                  More information about this error may be available in the server error log.
                  I can't seem to get anywhere with this... my script only includes the fields for the email and the actual file object, but my html form actually has several more fields. I only used the two fields in the script just to try and get it working. could this be why my script is not loading?

                  (im clutching at straws here) :(

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    Make sure to upload the script in ASCII (text) transfer mode and set the persmissions to 755 if necessary. The code you posted with the recommend change I made reports no syntax errors.

                    Comment

                    • owz2008
                      New Member
                      • Jun 2008
                      • 14

                      #11
                      I'm saving the script into notepad on a p.c, how do i go about making sure its in ASCII format? I have also set the permissions to 755...

                      Comment

                      • KevinADC
                        Recognized Expert Specialist
                        • Jan 2007
                        • 4092

                        #12
                        Originally posted by owz2008
                        I'm saving the script into notepad on a p.c, how do i go about making sure its in ASCII format? I have also set the permissions to 755...
                        Notepad saves in ASCII by default. I am talking about the transfer mode though, not the file format. If you use an FTP program to transfer the file from your PC to your website makes sure the file transfer mode is set to ASCII (or text) mode. See your FTP programs help files concerning the "transfer mode".

                        Comment

                        • owz2008
                          New Member
                          • Jun 2008
                          • 14

                          #13
                          Im wondering if perhaps the script is not finding perl at the location stated?

                          Also

                          What happens if the file upload path is incorrect? would that crash the script?

                          I've double checked the html and the form element's names (photo + email_address) both match the script...

                          Running out of ideas...

                          Comment

                          • KevinADC
                            Recognized Expert Specialist
                            • Jan 2007
                            • 4092

                            #14
                            Originally posted by owz2008
                            Im wondering if perhaps the script is not finding perl at the location stated?

                            Also

                            What happens if the file upload path is incorrect? would that crash the script?

                            I've double checked the html and the form element's names (photo + email_address) both match the script...

                            Running out of ideas...
                            Your other script appeared to work but could not find the here doc terminator. So the path to perl should be OK.

                            If the fileupload path is not correct::

                            Code:
                            open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
                            the "die" command in the above line will be triggered and you will see an error message about unable to open the file or directory, you can change it to:

                            Code:
                            open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "Can't open <$upload_dir/$filename> : $!";
                            that way you can see what $upload_dir/$filename is being interpolated as if the error occurs there, but I don't think so. Your script appears to be unable to compile, which makes me think the transfer mode was wrong.

                            Check the server error logs if possible. Most hosts allow you to see the error logs, either via FTP or via a control panel your host has installed for you to use.

                            Comment

                            • KevinADC
                              Recognized Expert Specialist
                              • Jan 2007
                              • 4092

                              #15
                              a side note, even if you get it to work the image will not display because the url to the image is wrong in the html code:

                              Code:
                              <img src="/upload/$filename" alt="Photo" />
                              should be:

                              Code:
                              <img src="../canvasimages/$filename" alt="Photo" />

                              Comment

                              Working...