need "force download" script
I need a script that forces the download of a Windows Media file.
We want to avoid the 'auto media plugin' response and have the file downloaded to the users hard drive. Everyone says the easy, "just zip it", --what if they don't have an "unzip" program... too complicating. Purpose: One result in this case, we are actually being kind by not forcing them to watch it right away in their browser ( -: They can have better control and watch at their leasure Thank you! FYI: No server side language being used. It would have to be a java script or a browser instruction in the HTML page. Back to top |
|||||
No server side language = no control over server behavior. This is easy to do with server side programming, just a few lines of code.
There's no way to control download behavior that I'm aware of without using a server side language, although there might be a javascript method. There's definitely no HTML method as far as I know. The way you do it with server side programming is send the click request to a page that simply contains the file name (or pass the file name as a variable, that's easier to maintain), then create a header that forces a download of the file instead of allowing the default browser behavior to occur. Back to top |
|||||
:: jeffd wrote :: No server side language = no control over server behavior. This is easy to do with server side programming, just a few lines of code.
There's no way to control download behavior that I'm aware of without using a server side language, although there might be a javascript method. There's definitely no HTML method as far as I know. The way you do it with server side programming is send the click request to a page that simply contains the file name (or pass the file name as a variable, that's easier to maintain), then create a header that forces a download of the file instead of allowing the default browser behavior to occur. JeffD: That makes total sense, because it needs to work an all browsers obviously. Can you point me to an example of how this is set-up? (language of the header, etc.) Thanks much. Back to top |
|||||
This is one way, it assume you are sending the filename to some page, let's call it download.php:
So the url is /utilities/download.php?folder=somefolder&file=somefilename.wmv :: Code :: <?php
$filename = ( isset($_GET['file']) ) ? $_GET['file'] : ''; $folder = ( isset($_GET['folder']) ) ? $_GET['folder'] : ''; if ( $filename && $folder )// make sure there's data in them { $file_info = basename($filename); $file_info = explode(".", $file_info); $extension = $file_info[1]; // notice here, you can't insert a different url to add scripts since you always are adding / before anything gotten from the query string. There's many, and better, ways, to protect yourself, but this one works fine, and is simple. $filename = '/' . $folder . '/' . $filename; header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/" . $extension ); header("Content-Type: application/download"); header("Content-Disposition: attachment; filename=".basename($filename).";"); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($filename)); readfile("$filename"); exit(); } ?> Or something like that, you have to test it a bit to make sure it works. When sending headers, no space or other html can be sent before the script executes, in other words, the download.php must begin with: <?php and end with ?> no line break or anything anywhere. That's a sloppy old script, it could be done better, and has been done better, but this is more or less how you do it. Back to top |
|||||
All times are GMT - 8 Hours
|