* @copyright 1997-2005 CodeCall / Jordan DeLozier * @link http://www.codecall.net */ /* * Place includes, constant defines and $_GLOBAL settings here. * Make sure they have appropriate docblocks to avoid phpDocumentor * construing they are documented by the page-level docblock. ***********************************************************************/ // Mod below - commented out unneeded crap # Disable all warnings #------------------------------------ /*error_reporting(0); # Get our file download information #------------------------------------ $file = $_GET['file']; $download = $_GET['download']; # Run Checks #---------------------------- if ($file == "") { print "Error: No File Selected"; die(); }*/ // Mod blow, added function header function startDownload($file, $download) { # Make sure program execution doesn't time out #------------------------------- set_time_limit(0); # Get the filename to be downloaded #---------------------------------- $file = stripslashes($file); $file = htmlspecialchars($file); # file extension #----------------------------------- $fext = strtolower(substr(strrchr($file,"."),1)); # Get the File Size #----------------------------------- //$fsize = remotefsize($file); # Get the mime type #------------------------------------ $mtype = getmtype($fext); # Check to see if the file is a link or download #------------------------------------------------- if ($download == 1) { header("Pragma: no-cache"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-type: $mtype"); header("Content-Transfer-Encoding: binary"); header("Content-Disposition: attachment; filename=\"".basename($file)."\""); if (ini_get('allow_url_fopen')) { if (extension_loaded('curl')) { header("Content-Length: " . remotefsize($file)); } } header("Accept-Ranges: bytes"); @readfile("$file"); } else { # Forward the user to the file #-------------------------------------------------- header("Location: " .$file); } } //////////////////////////////////////////////////////////// // Get the mtype //////////////////////////////////////////////////////////// function getmtype($fext) { # Mime Types #------------------------------- $mine_types = array ( # archives #------------------------------- 'zip' => 'application/zip', # documents #------------------------------- 'pdf' => 'application/pdf', 'doc' => 'application/msword', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint', # executables #------------------------------- 'exe' => 'application/octet-stream', # images #------------------------------- 'gif' => 'image/gif', 'png' => 'image/png', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', # audio #------------------------------- 'mp3' => 'audio/mpeg', 'wav' => 'audio/x-wav', # video #------------------------------- 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'mpe' => 'video/mpeg', 'mov' => 'video/quicktime', 'avi' => 'video/x-msvideo' ); # Find the Mime Type #------------------------------- if ($mine_types[$fext] == '') { $mtype = ''; # mime type is not set, get from server settings #------------------------------- if (function_exists('mime_content_type')) { $mtype = mime_content_type($file); } else { $mtype = "application/force-download"; } } else { # get mime type defined by admin #------------------------------- $mtype = $mine_types[$fext]; } # Return our Type #------------------------------- return $mtype; } //////////////////////////////////////////////////////////// // Filesize of remote file // Grab the filesize of a file hosted on another server //////////////////////////////////////////////////////////// function remotefsize($url, $user = "", $pw = "") { ob_start(); $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); if(!empty($user) && !empty($pw)) { $headers = array('Authorization: Basic ' . base64_encode("$user:$pw")); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } $ok = curl_exec($ch); curl_close($ch); $head = ob_get_contents(); ob_end_clean(); $regex = '/Content-Length:\s([0-9].+?)\s/'; $count = preg_match($regex, $head, $matches); return isset($matches[1]) ? $matches[1] : "unknown"; } // Old Code - PHP 5 only /* function remotefsize($url) { if (ini_get('allow_url_fopen')) { $schemed = parse_url($url); $sch = $schemed['scheme']; //$sch = parse_url($url, PHP_URL_SCHEME); if (($sch != "http") && ($sch != "https") && ($sch != "ftp") && ($sch != "ftps")) { return false; } if (($sch == "http") || ($sch == "https")) { $headers = get_headers($url, 1); if ((!array_key_exists("Content-Length", $headers))) { return false; } return $headers["Content-Length"]; } if (($sch == "ftp") || ($sch == "ftps")) { $parsed = parse_url($url); $server = $parsed['host']; $port = $parsed['port']; $path = $parsed['path']; $user = $parsed['user']; $pass = $parsed['pass']; /* PHP 5.0++ Code $server = parse_url($url, PHP_URL_HOST); $port = parse_url($url, PHP_URL_PORT); $path = parse_url($url, PHP_URL_PATH); $user = parse_url($url, PHP_URL_USER); $pass = parse_url($url, PHP_URL_PASS); if ((!$server) || (!$path)) { return false; } if (!$port) { $port = 21; } if (!$user) { $user = "anonymous"; } if (!$pass) { $pass = "phpos@"; } switch ($sch) { case "ftp": $ftpid = ftp_connect($server, $port); break; case "ftps": $ftpid = ftp_ssl_connect($server, $port); break; } if (!$ftpid) { return false; } $login = ftp_login($ftpid, $user, $pass); if (!$login) { return false; } $ftpsize = ftp_size($ftpid, $path); ftp_close($ftpid); if ($ftpsize == -1) { return false; } return $ftpsize; } } else { return false; } } */ ?>