Connecting Tech Pros Worldwide Forums | Help | Site Map

Force Download?

moishy's Avatar
Member
 
Join Date: Oct 2006
Posts: 104
#1: Feb 8 '08
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.

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#2: Feb 8 '08

re: Force Download?


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.
moishy's Avatar
Member
 
Join Date: Oct 2006
Posts: 104
#3: Feb 10 '08

re: Force Download?


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');
?>
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#4: Feb 10 '08

re: Force Download?


Quote:

Originally Posted by moishy

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!
Reply