Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Downloading a file

by JimJx (Beadle)
on Sep 09, 2006 at 03:02 UTC ( [id://572113]=perlquestion: print w/replies, xml ) Need Help??

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

hi all, I have 2 files that I need to let ppl dl. I have seen a couple examples here of how to do this, but I get the name of the script that I am using as the filename when the dl box pops. Any ideas on how to get around this? Thanks in advance, Jim
# $filepath is the directory # $filename is the name of the file my ($filepath,$filename) = @_; chdir($filepath) || return(0); my $filesize = -s $filename; # 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 STDOUT; while () { print; } close(READ); # should always return true return(1);

Replies are listed 'Best First'.
Re: Downloading a file
by imp (Priest) on Sep 09, 2006 at 04:23 UTC
    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);
Re: Downloading a file
by chromatic (Archbishop) on Sep 09, 2006 at 05:43 UTC
    my ($filepath,$filename) = @_;

    That code is probably wrong, but I can't tell because you didn't show how you're invoking this program or script or fragment or whatever. How do you get the filepath and filename from the request?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://572113]
Approved by sk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-23 23:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found