This is the latest version of my forced download class. This class is part of a much larger system so some slight tailoring will be needed. What I do is pass in an encrypted string which is the filename. The string is decrypted and a filepath appended to it.
It then ensures the filepath/filename is valid. If not, it forces the download of an image with an "error 404" message on it. if its valid, the user it prompted to download the file and you can programmatic-ally control how fast the download goes.
I've also put in a code block where you can override the mime types of certain file extensions if your web server doesn't do it properly for you. Really all you need to worry about is the "performDownload" method because that method accepts the clear-text path to the file to be downloaded.
<?php /** * This is the Force File Download class of the Skyward Portal * @package Skyward_Landing_Page * @subpackage Force File Download * @filesource * @author Matt Ford * @version 1.0 */ class com_forcedownload { /** * This method executes necessary methods based on request data * @access public * @author Matt Ford * @param array $data configuration and request information */ public function preProcess($data) { switch ($data->request["task"]) { case "forcefeed": default: $this->forceFeed($data); break; } } /** * This method determines if the file path is good and executes the necessary method * @access private * @author Matt Ford * @param array $data configuration and request information */ private function forceFeed ($data) { $file = $controller_config->downloadRootDir . $controller->decrypt($data->request["file"]); $this->performDownload($file); } else { $this->invalidFile($file); } } /** * This method performs the necessary actions to force the users browser to prompt a download box * @access private * @author Matt Ford * @param string $file path to file */ private function performDownload($file) { $download_rate = 10000; //kbps switch ($extension) { case "doc": case "docx": $mime = "application/msword"; break; case "exe": $mime = "application/octet-stream"; break; case "gif": $mime = "image/gif"; break; case "jpg": case "jpeg": $mime = "image/jpeg"; break; case "txt": $mime = "text/plain"; break; case "xls": case "xlsx": $mime = "application/excel"; break; default: $mime = "application/octet-stream"; break; } header ("Content-Disposition: attachment; filename=\"" . str_replace(" ", "_", basename($file)) . "\";"); } } /** * This method simply echoes an error message of a bad file/path * @access private * @author Matt Ford * @param string $file path to file */ private function invalidFile($file) { global $controller_config; //echo "\"" . basename($file) . "\" is either not a valid file or not in the specified path"; //die(); $this->performDownload($controller_config->path . "/supplementalFiles/img/404_File_Not_Found.jpg"); } } ?>
You need to login to post a comment.
