Force Download File

<?php function output_file($source, $name){ // Allow direct file download (hotlinking)? // Empty - allow hotlinking // If set to nonempty value (Example: example.com) will only allow downloads when referrer contains this text define('ALLOWED_REFERRER', ''); $file = $source; $filename_only = $name; $target_file = $file; // Get extension of requested file $extension = strtolower(substr(strrchr($file, "."), 1)); $filename_only .=".$extension"; // Determine correct MIME type switch($extension){ case "asf": $type = "video/x-ms-asf"; break; case "avi": $type = "video/x-msvideo"; break; case "bin": $type = "application/octet-stream"; break; case "bmp": $type = "image/bmp"; break; case "cgi": $type = "magnus-internal/cgi"; break; case "css": $type = "text/css"; break; case "dcr": $type = "application/x-director"; break; case "dxr": $type = "application/x-director"; break; case "dll": $type = "application/octet-stream"; break; case "doc": $type = "application/msword"; break; case "exe": $type = "application/octet-stream"; break; case "gif": $type = "image/gif"; break; case "gtar": $type = "application/x-gtar"; break; case "gz": $type = "application/gzip"; break; case "htm": $type = "text/html"; break; case "html": $type = "text/html"; break; case "iso": $type = "application/octet-stream"; break; case "jar": $type = "application/java-archive"; break; case "java": $type = "text/x-java-source"; break; case "jnlp": $type = "application/x-java-jnlp-file"; break; case "js": $type = "application/x-javascript"; break; case "jpg": $type = "image/jpg"; break; case "jpe": $type = "image/jpg"; break; case "jpeg": $type = "image/jpg"; break; case "lzh": $type = "application/octet-stream"; break; case "mdb": $type = "application/mdb"; break; case "mid": $type = "audio/x-midi"; break; case "midi": $type = "audio/x-midi"; break; case "mov": $type = "video/quicktime"; break; case "mp2": $type = "audio/x-mpeg"; break; case "mp3": $type = "audio/mpeg"; break; case "mpg": $type = "video/mpeg"; break; case "mpe": $type = "video/mpeg"; break; case "mpeg": $type = "video/mpeg"; break; case "pdf": $type = "application/pdf"; break; case "php": $type = "application/x-httpd-php"; break; case "php3": $type = "application/x-httpd-php3"; break; case "php4": $type = "application/x-httpd-php"; break; case "png": $type = "image/png"; break; case "ppt": $type = "application/mspowerpoint"; break; case "qt": $type = "video/quicktime"; break; case "qti": $type = "image/x-quicktime"; break; case "rar": $type = "encoding/x-compress"; break; case "ra": $type = "audio/x-pn-realaudio"; break; case "rm": $type = "audio/x-pn-realaudio"; break; case "ram": $type = "audio/x-pn-realaudio"; break; case "rtf": $type = "application/rtf"; break; case "swa": $type = "application/x-director"; break; case "swf": $type = "application/x-shockwave-flash"; break; case "tar": $type = "application/x-tar"; break; case "tgz": $type = "application/gzip"; break; case "tif": $type = "image/tiff"; break; case "tiff": $type = "image/tiff"; break; case "torrent": $type = "application/x-bittorrent"; break; case "txt": $type = "text/plain"; break; case "wav": $type = "audio/wav"; break; case "wma": $type = "audio/x-ms-wma"; break; case "wmv": $type = "video/x-ms-wmv"; break; case "xls": $type = "application/vnd.ms-excel"; break; case "xml": $type = "application/xml"; break; case "7z": $type = "application/x-compress"; break; case "zip": $type = "application/x-zip-compressed"; break; default: $type = "application/force-download"; break; } // Fix IE bug [0] $header_file = (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) ? preg_replace('/\./', '%2e', $filename_only, substr_count($filename_only, '.') - 1) : $filename_only; //Prepare headers header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public", false); header("Content-Description: File Transfer"); header("Content-Type: " . $type. "; charset=utf-8"); header("Accept-Ranges: bytes"); header("Content-Disposition: attachment; filename=\"" . $header_file . "\""); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . filesize($target_file)); // echo $target_file;exit; // Send file for download if ($stream = fopen($target_file, 'r')){ while(!feof($stream) && connection_status() == 0){ //reset time limit for big files set_time_limit(0); print(fread($stream,1024*8)); flush(); } fclose($stream); }else{echo "ERROR";exit;} } ?>
Read a web (or non-web accessible) file and output to the browser via download

1 Response

Nice Snippet.... But if user using Apache server then few lines in .htaccess do the same and its very easy. check out @ http://codepad.co/s/2d1ed1

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.