Download Any File Forcedly

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Everyone can customize following code for forcedly download any file.
Following change are needed:
1. Folder path.
2. File name.

Please visit http://www.kumarkhali.com and send your opinion.
Thanks everyone. 

<?php 
function OutputFile($file="", $dir="") {
	if(!is_readable($dir.$file)) 
		die('File not found or inaccessible!');
	$mimeType = "";
 	$file = rawurldecode($file);
 	$size = filesize($dir.$file);
	//rawurldecode('foo%20bar%40baz'); //Output: foo bar@baz
	//Def: Returns a string in which the sequences with percent (%) signs
followed by two hex digits have been replaced with literal characters
	$contentType = array(
		   	"pdf" => "application/pdf", 
		   	"exe" => "application/octet-stream", 
		   	"xls" => "application/vnd.ms-excel", 
		   	"ppt" => "application/vnd.ms-powerpoint", 
		   	"gif" => "image/gif", 
		   	"bmp" => "image/bmp",
		   	"png" => "image/png", 
		   	"jpg" => "image/jpg", 
		   	"jpeg" => "image/jpeg",
		   	"jpe" => "image/jpeg",
		   	"ico" => "image/x-icon",
		   	"mid" => "audio/midi",
		   	"midi" => "audio/midi",
		   	"kar" => "audio/midi",
		   	"mpga" => "audio/mpeg",
		   	"mp2" => "audio/mpeg",
		   	//"mp3" => "audio/mpeg",
		   	"mp4" => "audio/mpeg",
		   	"m3u" => "audio/x-mpegurl",
			"mp3" => "audio/mp3", 
		   	"wav" => "audio/x-wav", 
		   	"mpe" => "video/mpeg", 
		   	"mov" => "video/quicktime", 
		   	"avi" => "video/x-msvideo" , 
		   	"ram" => "audio/x-pn-realaudio",
		   	"rm" => "audio/x-pn-realaudio",
		   	"movie" => "video/x-sgi-movie",
		   	"ez" => "application/andrew-inset", 
		   	"hqx" => "application/mac-binhex40", 
		   	"cpt" => "application/mac-compactpro", 
		   	"mathml" => "application/mathml+xml", 
		   	"doc" => "application/msword", 
		   	"bin" => "application/octet-stream", 
		   	"dms" => "application/octet-stream", 
		   	"lha" => "application/octet-stream", 
		   	"lzh" => "application/octet-stream", 
		   	"class" => "application/octet-stream", 
		   	"so" => "application/octet-stream", 
		   	"dll" => "application/octet-stream", 
		   	"dmg" => "application/octet-stream", 
		   	"oda" => "application/oda", 
		   	"ogg" => "application/ogg", 
		   	"ai" => "application/postscript", 
		   	"eps" => "application/postscript", 
		   	"ps" => "application/postscript",
		   	"rdf" => "application/rdf+xml", 
		   	"smi" => "application/smil", 
		   	"smil" => "application/smil", 
		   	"gram" => "application/srgs", 
		   	"grxml" => "application/srgs+xml", 
		   	"mif" => "application/vnd.mif", 
		   	"xul" => "application/vnd.mozilla.xul+xml", 
		   	"wbxml" => "application/vnd.wap.wbxml",
		   	"vxml" => "application/voicexml+xml",
		   	"vcd" => "application/x-cdlink",
		   	"swf" => "application/x-shockwave-flash",
		   	"tar" => "application/x-tar",
		   	"tgz" => "application/x-tar",
		   	"zip" => "application/zip",
		   	"php" => "aapplication/x-httpd-php",
		   	"php4" => "aapplication/x-httpd-php",
		   	"php3" => "aapplication/x-httpd-php",
		   	"phtml" => "aapplication/x-httpd-php",
		   	"pdb" => "chemical/x-pdb",
		   	"js" => "application/x-javascript",
		   	"css" => "text/css",
		   	"html" => "text/html",
		   	"shtml" => "text/html",
		   	"htm" => "text/html",
		   	"asc" => "text/plain",
		   	"txt" => "text/plain",
		   	"rtx" => "text/richtext",
		   	"rtf" => "text/rtf"
		   	);
		   
		   	if($mimeType == ''){
		       	$fileExtension = strtolower(substr(strrchr($file, "."), 1));
			   	if(array_key_exists($fileExtension, $contentType)){
			      	$mimeType = $contentType[$fileExtension];
			   	} else {
			       	$mimeType = "application/force-download";
				}
			}
			@ob_end_clean(); //turn off output buffering to decrease cpu usage
 
			// required for IE, otherwise Content-Disposition may be ignored
			if(ini_get('zlib.output_compression'))
				ini_set('zlib.output_compression', 'Off');
 			header("Content-Length: " . filesize($dir.$file));
			header('Content-Type: ' . $mimeType);
 			header('Content-Disposition: attachment; filename="'.$file.'"');
 			header("Content-Transfer-Encoding: binary");
 			header('Accept-Ranges: bytes');
 
 			/* The three lines below basically make the download non-cacheable */
 			header("Cache-control: private");
 			header('Pragma: private');
			$curTime = date("D, d M Y H:i:s e");
			header("Expires: ".$curTime);
			
			//header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
			readfile($dir.$file);
}
/*********************************************
Example of use

Make sure script execution doesn't time out.
Set maximum execution time in seconds 
(0 means no limit).
**********************************************/	
set_time_limit(0);	
$dirName = "upFile/";//Change your directory path
$fileName = "Skill.doc";//change your filename
//$fileName = "bg.png";
//$fileName = "google.pdf";
//$fileName = "Despedida.mp3";
//$fileName = "MySQL.chm";
OutputFile($fileName, $dirName);//call function to download
?>




[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux