in reply to Downloading a file

The headers look ok. I tested it successfully with IE6 and Firefox1.5 on a WinXP machine with slight modifications and a sample .tgz file:
#!/usr/bin/perl use strict; use warnings; my $filename = 'pm.tgz'; my $filesize = -s $filename; # $filepath is the directory # $filename is the name of the file # print full header print "Content-disposition: inline; filename=$filename\n"; print "Content-Length: $filesize\n"; print "Content-Type: application/octet-stream\n\n"; # open in binmode open(READ,,'<',$filename) || die; binmode READ; # stream it out binmode binmode STDOUT; while (<READ>) { print; } close(READ); # should always return true return(1);
Is it possible that you printed a header before this function was called?

I would recommend the following minor changes :

  1. Use scalars for filehandles instead of barewords. Stricter scope, and auto-closes the file when the scalar is destroyed
  2. Use the 3 argument form of open
  3. Use 'or' instead of '||' for testing failure as it has lower precedence
Updated code:
#!/usr/bin/perl use strict; use warnings; my $filename = 'pm.tgz'; my $filesize = -s $filename; # $filepath is the directory # $filename is the name of the file # print full header print "Content-disposition: inline; filename=$filename\n"; print "Content-Length: $filesize\n"; print "Content-Type: application/octet-stream\n\n"; # open in binmode open my $read,'<',$filename or die "Failed to read $filename - $!"; + binmode $read; # stream it out binmode binmode STDOUT; while (<$read>) { print; } # should always return true return(1);

Replies are listed 'Best First'.
Re^2: Downloading a file
by bart (Canon) on Sep 09, 2006 at 06:34 UTC