title: Uploading file through perl CGI script
body: <p>I am trying to upload a file on localhost server (Apache 2.2 on Linux platform), but I am getting <strong>Internal Server Error 500</strong>.</p>
  
  <p>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.</p>
  
  <p>This is my HTML code:    </p>
  
  <pre><code>&lt;FORM ACTION="http://localhost/cgi-bin/upload.cgi" METHOD="post" ENCTYPE="multipart/form-data"&gt;
  &lt;INPUT TYPE="file" NAME="photo"&gt;
  &lt;INPUT TYPE="submit" NAME="Submit" VALUE="Submit Form"&gt;
  &lt;/FORM&gt;
  </code></pre>
  
  <p>This is my cgi script that is being placed at /var/www/cgi-bin where I have given full permission to this file.</p>
  
  <pre><code>#!/usr/bin/perl -w
  use CGI;
  
  $upload_dir = "/home/myuser/Desktop/upload";
  $query = new CGI;
  $filename = $query-&gt;param("photo");
  $filename =~ s/.*[\/\\](.*)/$1/;
  $upload_filehandle = $query-&gt;upload("photo");
  
  open UPLOADFILE, "&gt;$upload_dir/$filename";
  while ( &lt;$upload_filehandle&gt; )
  {
  print UPLOADFILE;
  }
  close UPLOADFILE;
  
  print $query-&gt;header ();
  
  print &lt;&lt;END_HTML;
  &lt;HTML&gt;
  &lt;HEAD&gt;
  &lt;TITLE&gt;Thanks!&lt;/TITLE&gt;
  &lt;/HEAD&gt;
  &lt;BODY&gt;
  &lt;P&gt;Thanks for uploading your photo!&lt;/P&gt;
      &lt;P&gt;Your photo:&lt;/P&gt;
  &lt;img src="/home/myuser/Desktop/upload/$filename" border="0"&gt;
  &lt;/BODY&gt;
  &lt;/HTML&gt;
  </code></pre>
  
  <p>Please somebody provide me a hint to solve the above simple problem. Thanks.</p>