Simple PHP download script!
April 5, 2012 10:50 AM Subscribe
I need a simple PHP script to force download a file. Something like download.php?file=/path/to/file or download.php?id=43. Can someone help me out?
I've searched long and hard on the internet for something, and despite how simple it is, it seems I cannot find what I am looking for! I run a music blog and occasionally get free downloads from artists/labels and I want to make d/ling the files easier for my guests.
I've searched long and hard on the internet for something, and despite how simple it is, it seems I cannot find what I am looking for! I run a music blog and occasionally get free downloads from artists/labels and I want to make d/ling the files easier for my guests.
force download sounds sketchy.
You want to let someone download a file, just link to it href=/path/to/file Assuming it's mp3 or whatever, the server recognizes it and the browser gets the right content type/mime type and asks the user to open or save the file.
posted by k5.user at 10:53 AM on April 5, 2012
You want to let someone download a file, just link to it href=/path/to/file Assuming it's mp3 or whatever, the server recognizes it and the browser gets the right content type/mime type and asks the user to open or save the file.
posted by k5.user at 10:53 AM on April 5, 2012
Response by poster: @inkyz: I just want to make it a one-click experience. I think some people get confused when they hit the download link and the MP3 just starts streaming in their browser.
posted by ascetic at 10:53 AM on April 5, 2012
posted by ascetic at 10:53 AM on April 5, 2012
Response by poster: @k5.user: not true, in modern browser it just streams the MP3 in their browsers. Force download is not sketchy, I have a very reputable blog.
posted by ascetic at 10:55 AM on April 5, 2012
posted by ascetic at 10:55 AM on April 5, 2012
Change the button to "Listen", and add text that says something along the lines of "Right Click -> Save As to download".
posted by inigo2 at 10:57 AM on April 5, 2012
posted by inigo2 at 10:57 AM on April 5, 2012
Response by poster: @inigo2: I am aware of that solution. I want something more automated.
posted by ascetic at 10:59 AM on April 5, 2012
posted by ascetic at 10:59 AM on April 5, 2012
Try setting the Content-Disposition header, as seen in this question: http://stackoverflow.com/questions/6794255/html-download-a-pdf-file-instead-of-opening-them-in-browser-when-clicked
posted by jozxyqk at 11:03 AM on April 5, 2012 [2 favorites]
posted by jozxyqk at 11:03 AM on April 5, 2012 [2 favorites]
In casually Googling for this I'm seeing tons of solutions involving setting a content-disposition HTTP header, which is how I recall doing this in the past. Are you not finding these or are they unsuitable for some reason?
posted by XMLicious at 11:05 AM on April 5, 2012
posted by XMLicious at 11:05 AM on April 5, 2012
What you are looking for is a way of delivering a file through HTTP headers. Here is a script I've used in the past, unfortunately I can't find the website I originally picked it up from:
I had a button that would route a user through this after checking that the file existed.
If you want the file to ONLY be accessible from this script, you need to mess around with your web server permissions which is a whole other ballgame.
posted by fearnothing at 11:12 AM on April 5, 2012
function send_file($file, $rate=0)
{
$finfo=finfo_open();
$filename=basename($file);
$file_mime=finfo_file($finfo,$filename);
$size=filesize($file);
finfo_close($finfo);
header("Cache-Control: Private");
header("Content-Type: $file_mime");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Length: $size");
}
I had a button that would route a user through this after checking that the file existed.
If you want the file to ONLY be accessible from this script, you need to mess around with your web server permissions which is a whole other ballgame.
posted by fearnothing at 11:12 AM on April 5, 2012
Oh, for that script to work - in Apache at least - $file must be the FULL path of the file.
posted by fearnothing at 11:22 AM on April 5, 2012
posted by fearnothing at 11:22 AM on April 5, 2012
Just a quick note to say, the id=[number] URL is a much better idea than download.php?file=/path/to/file - which is a great way of making your server hackable
posted by iotic at 12:48 PM on April 5, 2012 [1 favorite]
posted by iotic at 12:48 PM on April 5, 2012 [1 favorite]
« Older It just might be easier to build one from scratch.... | Brooklyn afternoon bar crawl – can it be done? Newer »
This thread is closed to new comments.
posted by inkyz at 10:52 AM on April 5, 2012