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.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!