CGI Perl upload script 500 internal server error

meinweb2

New Member
Messages
9
Reaction score
0
Points
0
I've been trying to construct a cgi perl upload script to upload PowerPoint files to a folder and have run into a brick wall. Specifically, it is a 500 Internal Server Error. I successfully tested the setup on my home server (running Apache 2.2 on Windows with Strawberry Perl installed).

I've tried changing the permissions on all the files and folders to 777, but I still consistently receive the following 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@adamspowerpoint.x10hosting.com 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.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
"

Here's the code for the index.html file which references a cgi script which does the actual uploading:

Code:
<!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>File Upload</title>
 </head>
 <body>
   <form action="upload.cgi" method="post"  
enctype="multipart/form-data">
     <p>Photo to Upload: <input type="file" name="photo" /></p>
     <p>Your Email Address: <input type="text" name="email_address" /></p>
     <p><input type="submit" name="Submit" value="Submit Form" /></p>
   </form>
 </body>
</html>

And here is the cgi perl script code within upload.cgi:

Code:
#!/usr/bin/perl
use strict; 
use CGI; 
use CGI::Carp qw ( fatalsToBrowser ); 
use File::Basename; 
$CGI::POST_MAX = 1024 * 2000; 
my $safe_filename_characters = "a-zA-Z0-9_.-"; 
my $upload_dir = "2010"; 
my $query = new CGI; 
my $filename = $query->param("file"); 
my $email_address = $query->param("email_address"); 
if ( !$filename ) 
{ 
 print $query->header ( ); 
 print "There was a problem uploading file (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("file"); 
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 file!</p> 
   <p>Your email address: $email_address</p> 
   <p>Your photo:</p> 
   <!--<p><img src="/2010/$filename" alt="file" /></p> -->
 </body> 
</html> 
END_HTML

Does anyone know what's wrong with my coding or is there something wrong with the server? (starka.x10hosting.com)

This is the URL to the upload page: http://adamspowerpoint.x10hosting.com/uptest/ (The rest of the site is password-protected).
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
uploaded file in ASCII mode?

Doesn't throw a 500 when I tried it on my site.
 

meinweb2

New Member
Messages
9
Reaction score
0
Points
0
I used FileZilla to upload the file... I don't know if it used ACSII mode.

CORRECTION:

In my attempts to correct any errors in my coding, I screwed up everything. I tested the upload function on my server (it failed with a 500 error) and then fixed it. However, I still get a 500 error...

Here is the corrected code for the index.html:

Code:
<!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>File Upload</title>
 </head>
 <body>
   <form action="upload.cgi" method="post"  
enctype="multipart/form-data">
     <p>Photo to Upload: <input type="file" name="photo" /></p>
     <p>Your Email Address: <input type="text" name="email_address" /></p>
     <p><input type="submit" name="Submit" value="Submit Form" /></p>
   </form>
 </body>
</html>
And the code for the upload.cgi:

Code:
#!/usr/bin/perl -wT
 
use strict;
use CGI;  
use CGI::Carp qw ( fatalsToBrowser );  
use File::Basename;  
 
$CGI::POST_MAX = 1024 * 25000;
my $safe_filename_characters = "a-zA-Z0-9_.-";  
my $upload_dir = "/2010";  
 
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="/2010/$filename" alt="Photo" /></p>  
 </body>  
</html>  
END_HTML
Same dang error. :( I'll check if it uploaded correctly now.
 

meinweb2

New Member
Messages
9
Reaction score
0
Points
0
Yes, it did upload in ASCII mode.

I first had the upload.cgi in a folder with my index.html, then moved it to the cgi-bin. I tried giving all the files and folders involved 777 permissions, but nothing has worked so far. Still getting the internal server error 500. Could it be a problem with the server itself and not my coding?
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Is there anything in the error log? Do you get the same error with a minimal CGI perl script?

Code:
#!/usr/bin/perl

print "content-type: text/plain\n\n";
print "hello";

Check that there's not a BOM messing up the shebang line.

Mode 755 are the most permissive permissions you should set on a directory or script. You don't want them to be world writable.

Could it be a problem with the server itself and not my coding?
If you're not certain, it's probably not a problem with the server.
 
Last edited:

meinweb2

New Member
Messages
9
Reaction score
0
Points
0
Yes, there are three things in the error log:
[Tue Apr 06 08:58:14 2010] [error] [client 68.41.93.111] File does not exist: /home/USERNAME/public_html/500.shtml
[Tue Apr 06 08:58:13 2010] [error] [client 68.41.93.111] File does not exist: /home/USERNAME/public_html/favicon.ico
[Tue Apr 06 08:58:10 2010] [error] [client 68.41.93.111] File does not exist: /home/USERNAME/public_html/favicon.ico

With 755 permissions, the example script you wrote worked fine. However, my script is still giving a 500 internal server error... It seems it is a problem with my coding since the script you wrote worked okay. What is/could be wrong with my code? (see my second post for the current code, I messed up the first post.)

I'm a newbie to web coding, so I have no idea what a BOM is or what a shebang line is... (is it the line that tells the script where perl is installed?)
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Doesn't throw a 500 when I tried it on my site.

It's not your code.
I cut and pasted to a file on my site, chmoded 0755, and called it directly. Got your message "There was a problem uploading your photo (try a smaller file)."

Add:

Shebang line is:

#!/usr/bin/perl

BOM is a 'byte order mark', a couple of non-printable characters some text processors put at the beginning of a file. Notepad++ does it (they have a setting to turn it off), not sure of other programs.

Try cut/paste the code from your post above into a new file using FileManager, chmod 0755, and call from web.
 
Last edited:

meinweb2

New Member
Messages
9
Reaction score
0
Points
0
Ah, yes, the culprit must be Notepad. Where is the setting to turn it off? I'd like to do all my scripting and uploading in windows (and especially w/notepad)...

While waiting for a reply, I restarted into Linux (Ubuntu--I dual-boot). I used gedit to redo all the coding, CHMODed all the scripts to 755, and it worked! Thanks a bunch for the help!
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
I am talking about Notepad++ not the Notepad that comes with Windows, not sure if we are talking about the same program.
In Notepad++, top menu, Encoding, check Encode in UTF-8 without BOM
 

meinweb2

New Member
Messages
9
Reaction score
0
Points
0
Thanks! I'll check into that. I think we can consider this topic solved.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Yes, there are three things in the error log:
[Tue Apr 06 08:58:14 2010] [error] [client 68.41.93.111] File does not exist: /home/USERNAME/public_html/500.shtml
[Tue Apr 06 08:58:13 2010] [error] [client 68.41.93.111] File does not exist: /home/USERNAME/public_html/favicon.ico
[Tue Apr 06 08:58:10 2010] [error] [client 68.41.93.111] File does not exist: /home/USERNAME/public_html/favicon.ico
Those are safe to ignore. The first means you haven't created a custom error page for the 500 status response, and the latter two mean you haven't created a site icon.

I'm a newbie to web coding, so I have no idea what a BOM is or what a shebang line is... (is it the line that tells the script where perl is installed?)
Note the links in my post:
Check that there's not a BOM messing up the shebang line.
Read my sig and follow the directions for information on what a BOM and shebang are. (Note: the shebang link has been updated. It originally pointed to a thread on common problems causing the error you were getting.)
 
Last edited:
Top