Home > Scripts > Force Download

Home    |    Candybar    DollMakers    RoomMakers    |    Roiworld    BBDI    Star Dolls    |    Puzzles    Smilies    Links   

Back to Scripts

Force download and cache media files script

(requirements: PHP)


Download Media files

An easy to use force file download script, especially useful for sites that have large media files and would like force people to download the files instead of having them run remotely off their web server. This is most commonly applied to mp3's, video clips, pdf files and more.

This script is used on the doll maker page to force download the doll gif/png file. (See the middle disk button below the doll)


Cache-Control: public

Instructs the proxies that these files are cachable, "Cache-Control: public" and allows proxies, gateways and the client browser to cache the files to reduce the bandwidth consumption to the server.


Content-Disposition: filename and attachment

Identify the file as an attachment.


Force Download Usage

http://www.yoursite.com/force-download.php?file=filepath

It is VERY STRONGLY recommended that the above functionality is only used internally and is not viewable from the web site since this would represent a security breach of the site.


Security

If you expose this in a URL you are essentially posting a large sign titled "Hack me!"

What to do? Use literal values to represent your files that you would access, thus a value of "1" would represent the file xyz.pdf, a value of "2" would represent the file abc.mp3, and so on. Thus the only DOWNLOADABLE files are those specifically HARD-CODED in your script.


File download script

Below is the force-download.php script, save it as a standalone script:
The force download script failed when the apache server enabled output compression. This has been fixed below, check to see if compression is enabled (zlib.output_compression), if so its turned off. This error ONLY occurs on some IE browsers. It seems that if the html page (or other file type) it receives will make it ignore the content disposition tag. Also setting the cache-control to public allows the file to be found (another IE quirk) (fixed Feb 16 2005)

An error was fixed in the script, $file-extension should be $file_extension. (fixed Oct 11 2004)

An error was fixed in the script, if the filename had spaces in it, it would not work
attachment; filename=".basename($filename).";" ); -> attachment; filename=\"".basename($filename)."\";" ); (fixed by Rajkumar Singh, Aug 18 2005)




This script is a modified version of the one listed by Ergon (full name unknown) on php.net/header, see references below


The extension extractor modification to the script ..strtolower(substr(strrchr($filename,"."),1)).. was contributed by Jorg Weske


To allow for filenames to contain spaces fix was contributed by Rajkumar Singh




References:
PHP.net, function header
RFC 2068, Header Field definitions
Web Caching tutorial for programmers and webmasters
List of MIME types
comments powered by Disqus


Creative Commons License All work by eLouai is licensed under a Creative Commons Attribution 3.0 Unported License
contact: elouai@gmail.com        ©2003-2014 eLouai.com, All rights reserved        Hosting by Web Hosting Canada        Privacy Policy        About