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

I'm trying to read a file that is sent via the filefield field (redundant :-p)and I cannot get anything. I'm using CGI.pm hoping against hope that the upload() func would work, but it doesn't seem to give me anything. The file is coming from a WinDoze box and the cgi in on a linux box. Here's the code I'm using to test with.
#!/usr/bin/perl $DEBUG = 0; ######## use CGI; $query = new CGI; ######## $type = 'something/else'; print $query->header(); $ERROR = $query->cgi_error; $fh = $query->upload('$filename'); $filename = escapeHTML($query->param('attfile')); $type = $query->uploadInfo($filename)->{'Content-Type'}; unless ($type eq 'text/html') { die "HTML FILES ONLY!"; } if (!$fh) { print_html($ERROR); exit 0; } else { print_html("The file was of type ", $type); } if($DEBUG == 1) { @Everything = <$fh>; print_html(@Everything); } sub print_html { @String_To_Use = @_; $SizeOfArray = $#_; $Count = 0; print $query->start_html(); print "<CENTER>\n"; while($Count <= $SizeOfArray) { print "$String_To_Use[$Count]\n"; print "<BR>\n"; $Count++; } print "</CENTER>\n"; print $query->end_html(); }
The output I get is:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META http-equiv=Content-Type content="text/html; charset=windows-1252 +"></HEAD> <BODY></BODY></HTML>
The file that I'm inputting, ie the filefield is C:\filename.txt

Any help is greatly appreciated.

Thanx

Ryan Howerton

Edit 2001-03-13 by tye to remove <PRE>.

Replies are listed 'Best First'.
Re: Reading a File Uploaded to a cgi script
by stephen (Priest) on Mar 13, 2001 at 22:57 UTC
    Your problem lies in the line:
    $fh = $query->upload('$filename');
    You have single quotes around $filename, thus preventing it from being interpreted. Get rid of them.
    $fh = $query->upload($filename);
    What it looks like is happening is that it was looking for a file field named, literally, '$filename'. It wasn't finding one-- yet the file got uploaded OK, so there was no error message. (I'm not sure why your "CENTER" tag didn't appear, though.) The following idiom might be clearer:
    eval { ## ... $fh = $query->upload($filename) or die "No file uploaded: '@{[ $query->cgi_error() ]}'"; ## ... }; $@ and do { print_html($@); };

    stephen

Re: Reading a File Uploaded to a cgi script
by mr.nick (Chaplain) on Mar 13, 2001 at 22:55 UTC
    Here is an example of a working upload html/script. Perhaps it will help you with yours.

    First the HTML file (upload.html):

    <html> <body> <form method=post action="upload.cgi" ENCTYPE="multipart/form-data"> <input type="file" name=foo> <input type="submit"> </form> </body> </html>
    and now the upload.cgi script:
    #!/bin/perl use CGI; my $self=new CGI; print $self->header; for $a ($self->param()) { print "$a ... ",$self->param($a),"<br>"; } $fh=$self->param('foo'); print while $_=<$fh>;
      Thanks for the help...problem solved.
Re: Reading a File Uploaded to a cgi script
by howard40 (Beadle) on Mar 13, 2001 at 22:56 UTC
    i had to handle an uploaded resume in one of the scripts i wrote... i did it by hand, instead of using the CGI.pm upload feature... this is how i did it. ($FORM is a hash populated with all incoming form fields, grabbed via CGI.pm's param() func.)

    My form looks a bit like this:
    <FORM ACTION="apply.cgi" METHOD="POST" ENCTYPE="multipart/form-data"> Resume: <INPUT TYPE="FILE" NAME="resume">
    and the parsing script:
    if ($FORM{'resume'}) { #read in the resume's data my ($data,$size); while( $size = read( $FORM{'resume'}, $data, 1024) ) { $FORM{'resume_data'} .= $data; $FORM{'resume_size'} += $size; } }
    $FORM{'resume'} contains the name of the uploaded file and $FORM{'resume_data'} contains the actual data uploaded

    OR

    you could simply write directly to a file with something similar to:
    open (FH, ">/tmp/resume.$$") || die; while (<$FORM{'resume'}>) { print FH; } close (FH);



    THE UNIX CAR: you have to rebuild the engine whenever you hang a new air freshener in the car.
Re: Reading a File Uploaded to a cgi script
by cei (Monk) on Mar 13, 2001 at 23:47 UTC
    Um, perhaps another thing. You're setting $type = $query->uploadInfo($filename)->{'Content-Type'}; and then checking $type against 'text/html'. But if your test source file ends in '.txt' then what is mapping it to be an HTML file? I would think the file coming in would consider itself text/plain.

    Am I missing something?

      I really couldn't tell you one way or the other. I pulled most of those statements directly from CGI.pm docs, made some modifications so they matched my variables and ran with it. Thanks for the input though.
Re: Reading a File Uploaded to a cgi script
by rhowerton (Novice) on Mar 13, 2001 at 23:51 UTC
    Thanks everyone for the input. Problem solved.