How to make download file forcefully and securely ?

#   _____ ____  ____  ____  _____   ____  ____  _      _      _     ____  ____  ____ 
#  /    //  _ \/  __\/   _\/  __/  /  _ \/  _ \/ \  /|/ \  /|/ \   /  _ \/  _ \/  _ \
#  |  __\| / \||  \/||  /  |  \    | | \|| / \|| |  ||| |\ ||| |   | / \|| / \|| | \|
#  | |   | \_/||    /|  \_ |  /_   | |_/|| \_/|| |/\||| | \||| |_/\| \_/|| |-||| |_/|
#  \_/   \____/\_/\_\\____/\____\  \____/\____/\_/  \|\_/  \|\____/\____/\_/ \|\____/
#

Warning – Be careful with this script i have modified this just for me but you can use it anyways.

 'pass_protected/myfile_02.zip',
	'2_docs' => 'pass_protected/myfile_32.zip',
	'2_js' => 'pass_protected/my_docs.zip'
);


$file_name = $_GET['file'];
$file_name = isset($zip_files[$file_name]) ? $zip_files[$file_name] : null;
$file_name = $file_name ? Config::ABS_PATH . $file_name : null;

if (!$file_name) {
	exit();
}



// make sure it's a file before doing anything!
if(file_exists($file_name)) {

	/*
		Do any processing you'd like here:
		1.  Increment a counter
		2.  Do something with the DB
		3.  Check user permissions
		4.  Anything you want!
	*/
	
	

	// required for IE
	if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off');	}

	// get the file mime type using the file extension
	switch(strtolower(substr(strrchr($file_name, '.'), 1))) {
		//case 'pdf': $mime = 'application/pdf'; break;
		case 'zip': $mime = 'application/zip'; break;
		//case 'jpeg':
		case 'jpg': $mime = 'image/jpg'; break;
		default: $mime = 'application/force-download';
	}
	header('Pragma: public'); 	// required
	header('Expires: 0');		// no cache
	header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
	header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
	header('Cache-Control: private',false);
	header('Content-Type: '.$mime);
	header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
	header('Content-Transfer-Encoding: binary');
	header('Content-Length: '.filesize($file_name));	// provide file size
	header('Connection: close');
	readfile($file_name);		// push it out
	exit();

}

?>
Share

Leave a Reply

Your email address will not be published. Required fields are marked *