July 23, 2010
PHP: open a file download dialog
Question by Hugh Valin
I have an MPEG file (.mpg) hosted in Amazon S3, that I want to link to a page I have, so the user will be able to download it from the page.
I have in my page a link:
bla bla“
The link to the file works when I right-click it and choose “Save Target As” , but I would like it to work also when I left click it, and that it will open a file download dialog. right now, a left click will direct to a page that has the video directly played in it (in FireFox) or just won’t load (in Internet Explorer).
I am working in PHP, why does this happen?
Answer by Starx
Use this:
download.php?movie=moviename.mpg
header('Content-disposition: attachment; filename='.$_GET['moviename.mpg']);
exit();
This will force the download box for whatever movie you have.