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

gaaaaahhhhhhhhhhhhhhh.......................

i have tried in vain to save files that are uploaded with my cgi script. im using the multipart/form-data in the HTML script and i have tried using cgi.pm (and not using it), but i only manage to create the file not write to it. that is, the file i create is always 0 bytes big.

it creates the file correctly with the right parameters,but like i said, its always 0 bytes in size.....what am i missing here ?

ps: im using cgi.pm 2.74 and perl 5.005
pps: the HD is not full
pps: here is an extract from my code: on the recieving end

my $file = $q->param("uploaded_file") || error($q,"No file received"); my $type = $q->uploadInfo($file)->{'Content-Type'}; my $buffer = ""; my $type =~ s!^image/([a-zA-Z]+)$!$1!; sysopen (OUTFILE,UPLOAD_DIR . $file, O_CREAT | O_EXCL); while ($bytesread=read($file,$buffer,1024)) { print OUTFILE $buffer; } CLOSE OUTFILE;
sigh..im obviously missing something here, but i cant find it....any help appriciated.

regards
kenneth johansen
kennethj@stud.cs.uit.no

Edit: chipmunk 2001-08-16

Replies are listed 'Best First'.
Re: saving a file uploaded with cgi...this is driving me insane.
by traveler (Parson) on Aug 17, 2001 at 01:23 UTC
    Try something like:
    my $file = $q->param("uploaded_file") || error($q,"No file received"); my $type = $q->uploadInfo($file)->{'Content-Type'}; my $buffer = ""; my $type =~ s!^image/([a-zA-Z]+)$!$1!; open OUTFILE, ">$upload_dir/$file" or die "Cannot open file"; while ($bytesread=read OUTFILE, $buffer, 1024) { print OUTFILE $buffer; } close OUTFILE;
    Or you could make the last part a bit simpler
    open OUTFILE, ">$upload_dir/$file" or die "Cannot open file"; while (<$file>) { print OUTFILE; } close OUTFILE;
    That should get you on the right track.

    HTH, --traveler UPDATE: Corrected stupidities due to doing two things at once.

Re: saving a file uploaded with cgi...this is driving me insane.
by Agermain (Scribe) on Aug 17, 2001 at 01:14 UTC
    I had a similar problem with a similar script... you may want to try using

    my $file_handle = $req->upload( $file ); and while ($bytesread=read($file_handle,$buffer,1024))

    instead of

    while ($bytesread=read($file,$buffer,1024))

    I don't fully understand the way CGI.pm uploads files, per se...

    Update: I changed some code. I realized what I originally said didn't make any sense. Sorry!


    andre germain
    "Wherever you go, there you are."

Re: saving a file uploaded with cgi...this is driving me insane.
by jryan (Vicar) on Aug 17, 2001 at 00:44 UTC
    I might be wrong but... I don't see a place where $buffer is getting modified other than at its declaration at my $buffer = "";. If thats true (and I'm not just being a newb), the  print OUTFILE $buffer; statement is printing "" to the output file each time through the loop. Thats why the file is getting created, but has a size of 0 bytes each time (as the size of "" tends to be 0 :) ).

      This part:

      while ($bytesread=read($file,$buffer,1024))

      Has the effect of repeatedly bringing 1Kbyte of data into $buffer from $file. The value returned by the read() is the number of bytes read, so once the filehandle is exhausted the while loop stops. In effect it just sucks the uploaded data through $buffer 1K at a time until it has traversed the whole filehandle.

      I agree that it's a slightly odd way round, compared to eg splice: i expect that's in order to allow exactly this construction.

      For more, see read or man CGI (from which this code mostly comes).