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?
Teh_CrazeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gripeCommented:
Try:

print "Content-type: application/mp3\n";
print "Content-disposition: attachment; filename=little-to-late.mp3\n\n";
while (<mp3>) {
    print;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.