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

I am having enormous fun trying to upload small text files with a cgi script, the idea being that the contents will be parsed and if valid will be sent to another script for processing. The script also has a text box for pasting in files, and it only attempts to use the uploaded file if the text box is left blank. Text pasted into the box works as expected, so the rest of the code seems OK. According to the CGI.pm man pages, this sort of thing should work:
my $file = $query->param("uploaded_file"); my $data; while (<$file>) { $data .= $_; } $return = &validate_text($data); if ($return == 1) { # do stuff... }
However, $data is always empty, and Data::Dumper reveals that the only information in $file is the basename of the file I've uploaded. Can anyone suggest where to look next whilst debugging? I am somewhat confused by this...

Replies are listed 'Best First'.
Re: CGI.pm file uploading
by eric256 (Parson) on Feb 02, 2005 at 20:49 UTC

    The docs for CGI recommend using the upload method instead of param for uploading files. Just use

    $fh = $query->upload('uploaded_file'); while (<$fh>) { print; }

    Instead of printing you should do what you want with the file.

    One thing that bit me just recently with CGI was that if you call new twice on it then uploaded files are gone. The filename still comes over, but the file itself is gone. So if you are working in a mod_perl environment, make sure you are calling CGI->new once and only once.


    ___________
    Eric Hodges
Re: CGI.pm file uploading
by Mutant (Priest) on Feb 02, 2005 at 17:57 UTC
    Did you set enctype='multipart/form-data' on the form tag? ie. form tag should look something like this:
    <form method="post" enctype="multipart/form-data" action="myscript.cgi +">
Re: CGI.pm file uploading
by talexb (Chancellor) on Feb 02, 2005 at 17:57 UTC
      <form accept-charset="latin1" enctype="multipart/form-data" onSubmit="return validationCheck(this);" method="post" action="upload.cgi">

    Check your HTML for something like my example above. I believe the POST method means you can read from STDIN. My guess is you are trying to use GET instead.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      This is all I see in the output that looks similar:
      <form method="post" action="/cgi-bin/msatfinder/msatfinder.cgi" enctyp +e="multipart/form-data">
      Thanks.

        When all else fails, look in the log file.

        Alex / talexb / Toronto

        "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: CGI.pm file uploading
by Animator (Hermit) on Feb 02, 2005 at 17:58 UTC
    Are you sure your HTML-form is correct (as in, does it has the correct enctype?)?
Re: CGI.pm file uploading
by kprasanna_79 (Hermit) on Feb 02, 2005 at 20:17 UTC
    Hai
    First thing u have to do is check
    <form method="post" enctype="multipart/form-data" action="filename.cgi">
    Some browsers pass the whole path to the file, instead of just the filename, so it's a good idea to strip off everything that includes backslashes (Windows browsers) and forward slashes (Unix browsers) and which might appear before the filename:
    $filename =~ s/.*[\/\\](.*)/$1/;
    check for the filename
    use binmode if the file is a binary file
    --prasanna.k