<?

include_once(dirname(__FILE__)."/include/lib/class.dowloand.php");

ob_start();

$filePath=dirname(__FILE__)."/data/doc";

$filename=rawurldecode(trim($_GET[filename]));

$path=$filePath."/".rawurldecode(trim($_GET[file]));


if (is_file($path)) {
	
	ob_start();

	$Conversion="mbString";
	if (!function_exists("mb_convert_encoding")) $Conversion="iconv";


	if ( ! file_exists($path) ) {
		echo "<html><body>No File!</body></html>";
		exit;
	};
	
	$filesize = filesize($path);
	$fp = fopen($path, "rb");
	$attachmentinfo['filedata'] = fread($fp, $filesize);
	fclose($fp);
		
	$extension=str_replace(".","",substr($path,-4));

	header('X-Powered-By:');
	header("Cache-control: max-age=31536000");
	header("Expires: " . gmdate("D, d M Y H:i:s",time()+31536000) . "GMT");
	header("Last-Modified: " . gmdate("D, d M Y H:i:s",$file[dateline]) . "GMT");
		
	switch ($Conversion){
		case "mbString":
			$output=mb_convert_encoding($filename, "utf8", mb_detect_encoding($filename, "big5", true));
		break;
		default:
			$output=iconv("big5","utf8",str_replace(" ","_",$filename));
		break;
	}		

	header("Content-disposition: attachment; filename= ".$output);
	header("Content-Length: $filesize");
	
	switch ($extension) {
		case "txt": $ctype="text/plain"; break;
		case "doc": $ctype="application/msword"; break;
		case "xls": $ctype="application/vnd.ms-excel"; break;
		case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
		case "pdf": $ctype="application/pdf"; break;
		case "exe": $ctype="application/octet-stream"; break;
		case "zip": $ctype="application/zip"; break;
		case "rar": $ctype="application/octet-stream"; break;
		case "gif": $ctype="image/gif"; break;
		case "jpg": case "jpeg": $ctype="image/jpg"; break;
		case "png": $ctype="image/png"; break;
		default: $ctype="unknown/unknown"; break;
	
	}
		
	header("Content-Type: $ctype");
	echo $attachmentinfo['filedata'];
	ob_end_flush();	
	
}

exit;

?>