Link to home
Start Free TrialLog in
Avatar of Teh_Craze
Teh_Craze

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of gripe
gripe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Teh_Craze
Teh_Craze

ASKER

Tried your code down to the last pixel.

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

Thanks. Your points.
what is your expected result?
It's giving me a 0kb file.
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.
Once again, error on my part. it's little-_too_-late.  not little-_to_-late.mp3
Just another question, how would I tell the browser how big the actual file is?
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
Nevermind, got it. Content-length: $size.

Thanks.
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.
Want to give me an example on stat()? Never used it before.
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