2

I am trying to upload a file on localhost server (Apache 2.2 on Linux platform), but I am getting Internal Server Error 500.

Server is running fine (as I am able to print all environment variables through CGI script), I am unable to find my way even after lot of googling.

This is my HTML code:

<FORM ACTION="http://localhost/cgi-bin/upload.cgi" METHOD="post" ENCTYPE="multipart/form-data">
<INPUT TYPE="file" NAME="photo">
<INPUT TYPE="submit" NAME="Submit" VALUE="Submit Form">
</FORM>

This is my cgi script that is being placed at /var/www/cgi-bin where I have given full permission to this file.

#!/usr/bin/perl -w
use CGI;

$upload_dir = "/home/myuser/Desktop/upload";
$query = new CGI;
$filename = $query->param("photo");
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = $query->upload("photo");

open UPLOADFILE, ">$upload_dir/$filename";
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;

print $query->header ();

print <<END_HTML;
<HTML>
<HEAD>
<TITLE>Thanks!</TITLE>
</HEAD>
<BODY>
<P>Thanks for uploading your photo!</P>
    <P>Your photo:</P>
<img src="/home/myuser/Desktop/upload/$filename" border="0">
</BODY>
</HTML>

Please somebody provide me a hint to solve the above simple problem. Thanks.

Olli
  • 752
  • 6
  • 20
bioinformatician
  • 364
  • 1
  • 12
  • 27
  • 3
    From the [Stack Overflow Perl FAQ](http://stackoverflow.com/questions/tagged/perl?sort=faq): [How can I troubleshoot my Perl CGI script?](http://stackoverflow.com/questions/2165022/how-can-i-troubleshoot-my-perl-cgi-script) – daxim May 25 '12 at 08:36

2 Answers2

0

Check the apache error log (often found in /var/log/httpd/error_log) for warnings or error messages. I suspect that the problem is that the upload directory is not writable by the web server.

Snorri
  • 446
  • 2
  • 10
0

print "Content-type: text/html\n\n"; This is a content-type header that tells the receiving web browser what sort of data it is about to receive — in this case, an HTML document. If you forget to include it, or if you print something else before printing this header, you'll get an "Internal Server Error" when you try to access the CGI program.

END_HTML is also missing at the EOF.

avngr
  • 323
  • 3
  • 12