http://www.perlmonks.org?node_id=273123


in reply to file upload

I though that the first argument to read needed to be a file handle. I tested your code and it didn't work anywhere (via browser or manually on a LINUX box), but as soon as I added the following lines, all was well:
open (IN, "$filename") || die ("$!"); # your code here # now change read to... while ($Bytes = read(IN, $Buffer, 1024)) { $BytesRead += $Bytes; print OUTFILE $Buffer; } close(OUTFILE); close(IN);
Give that a try.

HTH,
Chris

Replies are listed 'Best First'.
Re: Re: file upload
by antirice (Priest) on Jul 10, 2003 at 21:21 UTC

    Try the following:

    my $file; open($file,"<test.txt") or die "urg: $!"; while ($bytes = read($file,$buffer,1024)) { $bytesread+=$bytes; print OUTFILE $buffer; } close(OUTFILE); close($file);

    Also, $filename is probably being set by calling $filename = CGI::param('upload_file_field'); which returns a variable that acts as both a string and a filehandle. As for why it doesn't work for IE, we would need to know the version of IE and platform (if it's Mac and IE4.x, there's a known issue with uploading files...but I don't recall much beyond that) before we begin beating ourselves in the head over this. Since it works with other browsers, the problem seems to be with the version of IE.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      I am using CGI as you assumed. Sorry, I should have been more clear on that. The script does work on the Mac, althought I haven't tested it with IE4. It has successfully worked with MacOSX (Camino, IE, iCab, Safari), it also works fine on Linux (mozilla) and in Windows (Netscape). However, it is not working in Windows IE (XP/6.0) -- I'm trying to test on other Win/IE combos. So, it appears to be an IE issue.

      It does print the filename, but not the content (there's a 0K file), and the error log doesn't show anything.

      Also, this problem exists on two different servers:
      Apache/1.3.26 (Unix) PHP/4.2.3 mod_perl/1.27 on FreeBSD
      Perl 5.005_03

      Apache/1.3.27 (Unix) mod_ssl/2.8.11 OpenSSL/0.9.6b FrontPage/5.0.2.2510 PHP/4.3.0 mod_throttle/3.1.2 on Linux
      Perl 5.006

Re: Re: file upload
by choocroot (Friar) on Jul 10, 2003 at 21:17 UTC
    In fact, if he uses the CGI module and he gets $filename from the param() method, then the string is also a filehandle. I also made the mistake in Re: Upload a file from a web form ;)