Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have written a cgi page which uploads files and processes them successfully using localhost. The following is the code for the page asking user to select file to upload
<FORM ACTION="http://....etc/upload.cgi" METHOD="post" ENCTYPE="multipart/form-data"> File to upload: <INPUT TYPE="file" NAME="data"> Your email address: <INPUT TYPE=text" NAME="email_address"; <INPUT TYPE="submit" NAME="Submit" VALUE="Submit Form"> </FORM>
when i call the upload.cgi get a 500 internal error
use CGI; open (TEST, ">data_file"); $query = new CGI; $in_file=$query->param("data"); open (FHtest, "<$in_file"); while (<FHtest>) { print TEST $_; } print $query->header ( ); print <<end_TEST; <html><head><title>Checking Details</title> </head> other stuff </html> end_TEST
Any suggesstion to why this works on localhost but not on the main web? Thanks

Replies are listed 'Best First'.
Re: cgi upload files
by gellyfish (Monsignor) on Jun 17, 2005 at 09:28 UTC

    Yes, you are totally missing the point about the file upload, the parameter 'data' here does contain the filename yes but that is obviously only any use when the cgi program and the file are on the same machine. You want to be using the upload function which returns a filehandle to the uploaded data.See The CGI documentation for more on this.

    /J\

      the parameter 'data' here does contain the filename yes but that is obviously only any use when the cgi program and the file are on the same machine
      Well.... not quite. From the CGI docs (emphasis mine):
      $filename = $query->param('uploaded_file');
      Different browsers will return slightly different things for the name. Some browsers return the filename only. Others return the full path to the file, using the path conventions of the user's machine. Regardless, the name returned is always the name of the file on the user's machine, and is unrelated to the name of the temporary file that CGI.pm creates during upload spooling (see below).

      The filename returned is also a file handle. You can read the contents of the file using standard Perl file reading calls:

      # Read a text file and print it out while (<$filename>) { print; } # Copy a binary file to somewhere safe open (OUTFILE,">>/usr/local/web/users/feedback"); while ($bytesread=read($filename,$buffer,1024)) { print OUTFILE $buffer; }

      His use is still wrong, and the upload() method is clearer, though.

Re: cgi upload files
by kprasanna_79 (Hermit) on Jun 17, 2005 at 09:42 UTC
    Hai,
    Upto my knowledge you have to use this line
    $upload_handle = $query->upload("data");
    This will return file handle using which u can read and store the file on server. Later u can read and show it to user.

    Check the following link for more info.Upload File


    --Prasanna.K
      thanks for the tips. managed to correct the problem
Re: cgi upload files
by dorward (Curate) on Jun 17, 2005 at 09:19 UTC

    My bet would be that your script doesn't have permission to write files there, but that's just a guess. You should probably start at Troubleshooting Perl CGI scripts, and add a suitable "or die()" to your attempts to open file handles.