By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

Force Download?

moishy
100+
P: 104
How do you make a "Force Download" i.e. that instead of for instance having a mp3 file open in a new tab, rather it will prompt the download.
Feb 8 '08 #1
Share this Question
Share on Google+
3 Replies


Markus
Expert 5K+
P: 6,050
Use content disposition for this.
[php]
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>
[/php]
Taken from php.net/header

Take a look at this too.
Feb 8 '08 #2

moishy
100+
P: 104
Thanks!
It works!
One more question:
In the page you linked to in the above post, is says that the file may not contain blank lines before or after code.
My question is:

<?php
// May I place code here? if not, is there anywhere in this PHP tag that I may?
// May I place blank lines anywhere IN the PHP tags?
header('Content-disposition: attachment; filename=movie.mpg');
header('Content-type: video/mpeg');
readfile('movie.mpg');
?>
Feb 10 '08 #3

Markus
Expert 5K+
P: 6,050
Thanks!
It works!
One more question:
In the page you linked to in the above post, is says that the file may not contain blank lines before or after code.
My question is:

<?php
// May I place code here? if not, is there anywhere in this PHP tag that I may?
// May I place blank lines anywhere IN the PHP tags?
header('Content-disposition: attachment; filename=movie.mpg');
header('Content-type: video/mpeg');
readfile('movie.mpg');
?>
I believe you may place code that does not produce any output before the headers.

Experiment with it!
Feb 10 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.