Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

Perl - content-type:application/??? Forcing Downloads -- URGENT

When setting the content type to application/mp3  how would I set the filename and file size?

In PHP it's just header("Content-Disposition: filename: $file");

But how would I do this in perl.

I've tried:
open(mp3, "./little-to-late.mp3");

print "Content-type:application/mp3";
print "Content-Disposition: attachment; filename=little-to-late.mp3";
foreach (<mp3>) {
print;
}

but it doesn't seem to be working, any ideas on how this might be accomplished?
0
Teh_Craze
Asked:
Teh_Craze
  • 8
  • 5
1 Solution
 
gripeCommented:
Try:

print "Content-type: application/mp3\n";
print "Content-disposition: attachment; filename=little-to-late.mp3\n\n";
while (<mp3>) {
    print;
}
0
 
Teh_CrazeAuthor Commented:
Tried your code down to the last pixel.

Here's the output.
www.alpha-vii.com/mp3/fixme.dll
0
 
Teh_CrazeAuthor Commented:
AHK IT WORKS WHEN IN A DIFFERNT WINDOW. Crap.

Thanks. Your points.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
gripeCommented:
what is your expected result?
0
 
Teh_CrazeAuthor Commented:
It's giving me a 0kb file.
0
 
Teh_CrazeAuthor Commented:
it should be putting out the actual file.

like www.alpha-vii.com/mp3/little-to-late.mp3

the script is just a download handler that I plan to develop further.
0
 
Teh_CrazeAuthor Commented:
Once again, error on my part. it's little-_too_-late.  not little-_to_-late.mp3
0
 
Teh_CrazeAuthor Commented:
Just another question, how would I tell the browser how big the actual file is?
0
 
gripeCommented:
ah, glad you got it fixed. good luck with your handler. I would suggest that you use CGI.pm for perl CGI code.. it's full of good features and it's included standard. perldoc CGI for more info.

Also, this is an excellent free book on Perl (Not just for beginner's) that contains an entire chapter on CGI:

http://learn.perl.org/library/beginning_perl
0
 
Teh_CrazeAuthor Commented:
Nevermind, got it. Content-length: $size.

Thanks.
0
 
gripeCommented:
Do you mean prior to download? (Like on a 'Download this: ' page?)

Just stat the file. stat() will return a list of values related to the file. The 7th list item (0 being the first) is the size in bytes.
0
 
Teh_CrazeAuthor Commented:
Want to give me an example on stat()? Never used it before.
0
 
gripeCommented:
Sure, given that your filehandle is in 'MP3':

my $size = (stat(MP3))[7];

or:

my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks)= stat($filename);

or:

my @stat_info = stat($filename);

Type:

perldoc -f stat

at your command line for an in depth description of everything.

This also works for any perl builtin function or module:

perldoc Module::Name
perldoc -f builtin_function
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now