By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,242 Members | 1,312 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,242 IT Pros & Developers. It's quick & easy.

CGI file upload script problems

P: 14
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.co.uk/canvaspayform.html

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 misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster@icomworks.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

Expand|Select|Wrap|Line Numbers
  1. <form name="contactdet" action="myscripts/upload.cgi"method="post" onSubmit="return validate_form ( );" enctype="multipart/form-data">
  2.                           <p>First name: <input type="text" name="name1" id="name1" size="20"/></p>
  3.                           <p>Last name: <input type="text" name="name2" id="name2" size="20"/></p>
  4.                           <p>Email address:<input type="text" name="email" id="email_address" size="20" /></p>
  5.                           <p>Contact number:<input type="text" name="phone" id="phone" size="20"/></p>
  6.                           <p>House number: <input type="text" name="housenum" id="housenum"/></p>
  7.                           <p>Address 1:<input type="text" name="add1" id="add1" /></p>
  8.                           <p>Address 2:<input type="text" name="add2" id="add2" /></p>
  9.                           <p>Town:<input type="text" name="town" id="town" class="town"/></p>
  10.                           <p>Post code:<input type="text" name="pcode1" id="pcode" size="10" class="pcode1"/></p>
  11.  
  12.                        <p>Picture upload:<input type="file" name="photo" /></p>      
  13.                           <p><input type="submit" name="submit" value="Upload Canvas Image" /></p></form>

and this is the cgi script I created:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -wT 

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


  2. $CGI::POST_MAX = 1024 * 5000; 

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

  4. my $upload_dir ="public_html/canvasimages"; 


  5. my $query = new CGI; 

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

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

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

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

  12.  
  13. 
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"; 
} 


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

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


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

close UPLOADFILE; 

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

  20. <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" /> 
* *
  21. <title>Thanks!</title> 
* *
  22. <style type="text/css"> 
* * *img {border: none;} 
* *</style> 
*</head> 
*
  23.  
  24. <body> 
* *
  25. <p>Thanks for uploading your photo!</p> 
* *
  26. <p>Your email address: $email_address</p> 
* *
  27. <p>Your photo:</p> 
* *
  28. <p><img src="/upload/$filename" alt="Photo" /></p> 

  29. *</body> 

  30. </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
Jun 24 '08 #1
Share this Question
Share on Google+
24 Replies


KevinADC
Expert 2.5K+
P: 4,059
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.
Jun 24 '08 #2

P: 14
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
Jun 24 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
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.
Jun 24 '08 #4

P: 14
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?
Jun 25 '08 #5

P: 14
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..

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -wT 
  2.  
  3. use strict; 
  4. use CGI; 
  5. use CGI::Carp qw ( fatalsToBrowser ); 
  6. use File::Basename; 
  7.  
  8. $CGI::POST_MAX = 1024 * 5000; 
  9. my $safe_filename_characters = "a-zA-Z0-9_.-"; 
  10. my $upload_dir = "/public_html/canvasimages"; 
  11.  
  12. my $query = new CGI; 
  13. my $filename = $query->param("photo"); 
  14. my $email_address = $query->param("email_address"); 
  15.  
  16. if ( !$filename ) 
  17.  print $query->header ( ); 
  18.  print "There was a problem uploading your photo (try a smaller file)."; 
  19.  exit; 
  20.  
  21. my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' ); 
  22. $filename = $name . $extension; 
  23. $filename =~ tr/ /_/; 
  24. $filename =~ s/[^$safe_filename_characters]//g; 
  25.  
  26. if ( $filename =~ /^([$safe_filename_characters]+)$/ ) 
  27.  $filename = $1; 
  28. else 
  29.  die "Filename contains invalid characters"; 
  30.  
  31. my $upload_filehandle = $query->upload("photo"); 
  32.  
  33. open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!"; 
  34. binmode UPLOADFILE; 
  35.  
  36. while ( <$upload_filehandle> ) 
  37.  print UPLOADFILE; 
  38.  
  39. close UPLOADFILE; 
  40.  
  41. print $query->header ( ); 
  42. print <<END_HTML; 
  43.  
  44.  
  45. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> 
  46. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
  47.  <head> 
  48.    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  49.    <title>Thanks!</title> 
  50.    <style type="text/css"> 
  51.      img {border: none;} 
  52.    </style> 
  53.  </head> 
  54.  <body> 
  55.    <p>Thanks for uploading your photo!</p> 
  56.    <p>Your email address: $email_address</p> 
  57.    <p>Your photo:</p> 
  58.    <p><img src="/upload/$filename" alt="Photo" /></p> 
  59.  </body> 
  60. </html> 
  61. END_HTML
Jun 25 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
Replace this:

Expand|Select|Wrap|Line Numbers
  1. print <<END_HTML; 
  2.  
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> 
  5. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
  6.  <head> 
  7.    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  8.    <title>Thanks!</title> 
  9.    <style type="text/css"> 
  10.      img {border: none;} 
  11.    </style> 
  12.  </head> 
  13.  <body> 
  14.    <p>Thanks for uploading your photo!</p> 
  15.    <p>Your email address: $email_address</p> 
  16.    <p>Your photo:</p> 
  17.    <p><img src="/upload/$filename" alt="Photo" /></p> 
  18.  </body> 
  19. </html> 
  20. END_HTML
replace with:

Expand|Select|Wrap|Line Numbers
  1. print qq{ 
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> 
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
  5.  <head> 
  6.    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  7.    <title>Thanks!</title> 
  8.    <style type="text/css"> 
  9.      img {border: none;} 
  10.    </style> 
  11.  </head> 
  12.  <body> 
  13.    <p>Thanks for uploading your photo!</p> 
  14.    <p>Your email address: $email_address</p> 
  15.    <p>Your photo:</p> 
  16.    <p><img src="/upload/$filename" alt="Photo" /></p> 
  17.  </body> 
  18. </html> 
  19. };
Then you don't have to worry about your HERE docs being terminated properly.
Jun 25 '08 #7

P: 14
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.co.uk/canvaspayform2.html instead of putting the actual html within the script.. how is this done?


thanks again, I appreciate the help..

Owain
Jun 26 '08 #8

P: 14
I tried replacing the code and I know get a different error?

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster@icomworks.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) :(
Jun 26 '08 #9

KevinADC
Expert 2.5K+
P: 4,059
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.
Jun 26 '08 #10

P: 14
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...
Jun 26 '08 #11

KevinADC
Expert 2.5K+
P: 4,059
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".
Jun 26 '08 #12

P: 14
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...
Jun 26 '08 #13

KevinADC
Expert 2.5K+
P: 4,059
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::

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. 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.
Jun 26 '08 #14

KevinADC
Expert 2.5K+
P: 4,059
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:

Expand|Select|Wrap|Line Numbers
  1. <img src="/upload/$filename" alt="Photo" />
should be:

Expand|Select|Wrap|Line Numbers
  1. <img src="../canvasimages/$filename" alt="Photo" />
Jun 26 '08 #15

P: 14
I think we're getting somewhere now..

i put the code in and replaced the paths, but I now get the error

Software error:

Can't open </public_html/canvasimages/louise_h_pic.jpg> : No such file or directory at upload.cgi line 39.

The file paths for the upload are not right. But im unsure of the convention..

On the ftp where i upload my site there sits the cgi - bin, public_html, cgi-data and logs, the directory canvasimages is where I want the uploaded image to go to. this is stored in directly in the public_html directory so I'm guessing that the file path would be:

"/public_html/canvasimages/image.jpg"

am I right in thinking that?
Jun 27 '08 #16

P: 14
I've also noticed that even when I get that last error, I check the canvasimages directory on the ftp and there are no images stored. ??
Jun 27 '08 #17

KevinADC
Expert 2.5K+
P: 4,059
generally there is more path data before the public_html folder, it is something like:

home/user/public_html

where user is your unique ID on the server, but there coul dbe more folders too, like on one server I rent the path to the public_html folder is:

/home/users/web/****/ipw.e-pixsco/public_html

run this script on your server:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl 
  2. print "Content-type: text/html\n\n"; 
  3. foreach $key (sort keys(%ENV)) {
  4.       printf("<font color=red>%-10.20s:</font> <font color=blue><b>$ENV{$key}</b></font><br>", $key);
  5.    }
  6. exit; 
It will display the ENV details of the server, the one named DOCUMENT_ROOT will be the full path to the public_html folder.
Jun 27 '08 #18

P: 14
Hi Kev, I tried that script but i got the following:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster@icomworks.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'm gonna have the word with the server administrator when he's in and see what he can do..

I have saved your script you did on notepad on a p.c and put it on the server, then I just typed the url to that script...

I made sure all the permissions were set to 755 but no luck

Im confused by this last error because I thought I was getting somewhere with my script but getting that error message on your script makes me think i'm doing something wrong my end...
Jun 27 '08 #19

P: 14
it might be worth me emailing you my script file and you can then tell me if the script is saved in the correct format... ?
Jun 27 '08 #20

KevinADC
Expert 2.5K+
P: 4,059
it might be worth me emailing you my script file and you can then tell me if the script is saved in the correct format... ?
Your script appears to be working, just the path to the public_html folder needs to be worked out. The script I posted for you to try should work, if it does not then you need to make sure to upload in ASCII (text) mode. That is different than the format you save the file in. See your FTP programs help files for "transfer mode" or similar.
Jun 27 '08 #21

P: 14
Hello again Kevin, I looked up how to upload in ascii format and it looks like I was uploading in ascii format all along, so thats not the issue,

I've decided to get a new script as I got fed up trying to debug the old one, but I'm now having problems with this news script...

I downloaded the file uploader script from www.mycgiscripts.com/file-uploader.html

theres two parts that have stumped me, this script now emails the user/admin to let them know that the upload has been successful, there is a few lines on the cg i script which are asking me for either my sendmail or an smtp address,

I checked with the newtwork admin and he said we do not use smtp, we use exchange to run our email system. here is the email lines in the script

#Mailing. Send emails to admin/user via smtp or sendmail? Set only one!

$sendmail="owain@icomworks.co.uk";
$smtp="";

would this be the correct format?
Jul 3 '08 #22

KevinADC
Expert 2.5K+
P: 4,059
this line:

$sendmail="owain@icomworks.co.uk";

should be:

$sendmail="owain\@icomworks.co.uk";

or use single-quotes so the @ symbol is not interpreted as an array and expanded into a string because of the double-quotes:

$sendmail='owain@icomworks.co.uk';
Jul 3 '08 #23

P: 14
Thanks Kevin, Iv'e done that. Not sure if it works yet as I'm still having trouble submitting the form..

I now seem to get the error "Error : Cannot upload file 1215417988-1jpg: Permission denied"

The file name is paypal.jpg is it normal for the files to be uploaded as different names?

I have checked the permissions on the uploadfolder, the script folder and the .cgi script, but it still produces this error?

any ideas why?
Jul 7 '08 #24

KevinADC
Expert 2.5K+
P: 4,059
make sure the folder you are uploading to has read/write permissions. That should be 644. You can check the folders permissions using an FTP program or your websites control panel, if there is one.
Jul 7 '08 #25

Post your reply

Sign in to post your reply or Sign up for a free account.