in reply to Re: Re: file uploading
in thread file uploading

It is well documented in the CGI documentation as well as here in the Monastery. Remember that Super Search is your friend.

Replies are listed 'Best First'.
Re: Re: Re: Re: file uploading
by Anonymous Monk on Apr 15, 2003 at 17:59 UTC
    Actually I did use both sources and didn't find any helpful information. I typically frown own reading module information on CPAN and elsewhere because they're not really helpful, you need to look at premade scripts to see why things work. Below is my new file, I changed it to save to a file 'text.txt' and it doesn't work. It makes the file text.txt but it has zero bytes, looks like all it does is create the file. It never shows what kind of file you need to open for this to work so I tossed in the text file, is this wrong?
    use warnings; use CGI qw/:standard/; use CGI::Carp 'fatalsToBrowser'; use POSIX; print header, start_html('upload form'); my $filename = "text.txt"; binmode($filename); print start_form(), table( Tr( td("File: "), td(filefield(-name=>'upload', -size=>50, -maxlength=>80), ), ), Tr( td(), td(submit('button','submit'), ) ) ), end_form(), hr; if (param()) { my $upload = param('upload'); while (<$filename>) { print; } open (OUTFILE,">>/home/myname/public_html/upload/text.txt") || die + $!; binmode(OUTFILE); while ($bytesread=read($filename,$buffer,1024)) { print OUTFILE $buffer; print "the thing is open"; } print "The file should be uploaded if everything went right, which it +probably didn't.\n"; }

      May I suggest trying the documentation again? CGI.pm is exceedingly well documented. The examples you copied are highly incorrect. In particular:

      my $filename = "text.txt"; binmode($filename);

      Calling binmode on a string doesn't do anything useful -- it only does something interesting on filehandles.

      while (<$filename>) { print; }

      Attempting to read from a string doesn't do anything useful -- it's a symbolic reference, but since you don't open a filehandle anywhere called 'text.txt', you won't get any data.

      while ($bytesread=read($filename,$buffer,1024)) { print OUTFILE $buffer; print "the thing is open"; }

      Again, $filename isn't a filehandle or even a symbolic reference to a filehandle. Even if it were, you'd have read everything from it in the while loop above. Besides that, mixing buffered reads with unbuffered generally mixes things up.

      The CGI documentation is *really* good about explaining how to receive uploaded files. It's well-worth your time to figure out how to read it rather than trying to copy and paste several other scripts together. That's obviously not working here. Why not try something different?

      I think you may be slightly confused on the whole files thing. A file is simply a bunch of bytes stored in a accessible little package on a drive - there's no 'magic' involved. A 'text' file is just a file which (a) will generally only contain nice 'readable' ASCII characters (plus some line endings) and (b) *may* indicate that it contains this by having a 'txt' or 'asc' or somesuch extension.

      When a browser uploads a file to your server as part of a form, it simply reads the local bytes, munges them all into an HTTP-friendly packet and sends off the whole lot. What CGI.pm does for you is split off those bytes, save them to a temporary file and then give you access to a filehandle - basically a way of reading the bytes back in from the temporary file and doing somewhere else with them - writing back out to a file of your choosing, parsing, dividing-by-the-number-you-first-thought-of...whatever.

      In order to have some idea of what the file actually contains - whether it's bunch of bytes that make up a picture, or the aforementioned nice'n'readable text file, you either need to examine the also-supplied-by-CGI.pm MIME type for the file, which will tell you whether it's a gif, an mp3, a text file or whatever. Generally, one does this in order to have an idea of the extension to then give the filename - never trust user-supplied filenames :)

      Hope this helps

      Ben