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

I'm thoroughly stumped. I've got a web app using CGI & CGI::Session where i'm trying to upload a csv file and process the data and parse it out to eventually store in a db. Here's my code:
sub thisupload{ my $self = shift; my $q = $self->query; my $file = $q->param('file'); my $error; if($file) { my $data; my $fileHandle = $q->upload($file); while(<$fileHandle>) { $data .= $_; } if($data) { $error = 'Successfull upload'; } else { $error = "Nothing uploaded<br>$fileHandle<br>$ +file<br>".cgi_error."<br>$!"; } } return $self->template({ file => 'getFile.html', vars => {erro +r => $error,}}); }
and my html template:
<form action="[% userbase %]/user.pl" method="POST" name="form1" encty +pe="multipart/form-data"> <input type="hidden" name="rm" value="thisupload"> File: <input type="file" name="file"/> <input type="submit" value="Submit"> </form>
$error returns:
Nothing uploaded

H:\test.txt

Operation now in progress
All the solutions I've found don't apply. I'm already using method="POST" and enctype="multipart/form-data". Ironically, I have another web app on the same server that uses similar code that works fine (that's where i got mine), so I can assume (???) that the server is configured correctly (permissions, CGI version, etc). I haven't been able to figure out this error or why $q->upload is undefined. Ideas???

Replies are listed 'Best First'.
Re: file upload undefined: Operation now in progress
by kdj (Friar) on Nov 25, 2008 at 20:23 UTC

    According to the docs:  When called with the name of an upload field, upload() returns a filehandle, or undef if the parameter is not a valid filehandle.

    I'm not able to test it at the moment, but I should think that your problem is that you are sending it the actual file name, and not the field that contains the file name.

    my $filehandle = $q->upload('file'); should work.
      Sorry...forgot to mention that I tried that too and it doesn't work either. I found some example code that used that, so I decided to give it a whirl, but no luck. My original code snippet has transitioned while trying everything I can find.

      If I call $q->tmpFileName(), it's undefined as well. Is upload() supposed to be saving to /tmp? My permissions are drwxrwxrwt on /tmp. Is that what I need? I'm not able to find much detailed info in any doc.

      Error log says: readline() on unopened filehandle at /var/www/perl/modules/Lockbox/User.pm line 1833.

        Even though, my other web app is still working, I found something that said some of the versions are screwed up & try v3.10 (was using the latest & greatest at v3.42). ...upload() is still undefined...

        What am I overlooking???

        ...still working on this...

        I've installed CGI::FileUpload to see where that gets me. Here's my current code:

        sub customerupload{ my $self = shift; my $q = $self->query; my @data; my $error = ""; my $filename = $q->param('uploadfile'); my $file; if($filename) { my $filehandle = new CGI::FileUpload(); $file = $filehandle->upload('query'=>$q); my $data; while(<$file>) { $data .= $_; } if($data) { $error = 'Successfull upload'; } else { $error .= $CGI::VERSION."Nothing read<br>$file +name<br>$file<br>"."<br>$!<br>"; } } else{$error .= $CGI::VERSION."file not uploaded <br>$filename< +br>$file<br>"."<br>$!<br>";} return $self->template({ file => 'customers.html', vars => {er +ror => $error, }}); }
        However, it crashes at the line in CGI::FileUpload.pm that uses the CGI::upload method (back to square 1):
        my $fhin=CGI::upload('uploadfile')||CORE::die "cannot convert [$filena +me] into filehandle: $!";
        The error message actually returns the name of my file i'm trying to upload. The CGI::FileUpload is creating the tmp files in /tmp/CGI-FileUpload dir: the key file (0 K), the key.part file (0 K), and the key.properties file (creates the file_orig, from_id, from_ipaddr, key, pid, and upload_status fields).

        don't know if I'm getting anywhere or not...

        OMG!!!

        I finally figured it out. I had an extra unclosed form tag in my HTML that was throwing it off.