php header 头输出 不同文档

function _serveFile($key) {
		ini_set("memory_limit","128M");
		$file = $this->_data[$key];
		if(isset($file) && !empty($file)) {
			$ext = end(explode(".", $file));
			$allowed = array("doc","rtf","txt","pdf","xls","jpg","png","tiff","jpeg");
			//if(in_array($ext,$allowed)) {
			if(1) {
				switch($ext) {
					case "txt":
						$type = "text/plain";
						break;
					case "rtf":
						$type = "application/rtf";
						break;
					case "doc":
						$type = "application/msword";
						break;
					case "docx":
						$type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
						break;	
					case "xls":
						$type = "application/vnd.ms-excel";
						break;
					case "pdf":	
						$type = "appliction/pdf";
						break;
					case "png":	
						$type = "image/png";
						break;	
					case "gif":	
						$type = "image/gif";
						break;	
					case "jpg":
					case "jpeg":	
						$type = "image/jpeg";
						break;	
					case "tiff":	
						$type = "image/tiff";
						break;		
					default:
						$type = "application/octet-stream";
						break;		
				}
				$name = $this->_data['first_name']."_".$this->_data['last_name']."_".$key.".".$ext;
				$tmp = $this->_createDecryptedTmpFile(UPLOAD_DIR.DS."files".DS.$file);
				
				
				header("Pragma:public");
				header("Expires:0"); 
				header("Cache-Control:must-revalidate,post-check=0,pre-check=0"); 
				header("Cache-Control:private",false);
				header("Content-Type:application/force-download");
				header("Content-Type:application/download");
				header("Content-Description:File Transfer");
				header("Content-type:".$type);
				header("Content-disposition: attachment; filename=".$name);
				header("Content-Length:".filesize($tmp));
				
				
				echo file_get_contents($tmp);
			} else {
				echo "The file type $ext is not allowed";
			}	
		}
	}
<?php
$File_Name=$_GET['file'];$File_Type = substr($File_Name, -4);switch ($File_Type){  Case ".mp3":  $ContentType = "audio/mpeg3";  $File_Path="Music_Library/";  $File_Location=$File_Path.$File_Name;  break;  Case ".avi":  $ContentType = "video/avi";  $File_Path="Videos/";  $File_Location=$File_Path.$File_Name;  break;  Case (".doc" OR "docx"):  $ContentType = "application/msword";  $File_Path="Music_Library/";  $File_Location=$File_Path.$File_Name;  break;  Case ".gif":  $ContentType = "image/gif";  $File_Path="Pictures/";  $File_Location=$File_Path.$File_Name;  break;  Case (".jpg" OR "jpeg"):  $ContentType = "image/jpeg";  $File_Path="Pictures/";  $File_Location=$File_Path.$File_Name;  break;  Case ".wav":  $ContentType = "audio/wav";  $File_Path="Music_Library/";  $File_Location=$File_Path.$File_Name;  break;  Case ".m4a":  $ContentType = "audio/m4a";  $File_Path="Music_Library/";  $File_Location=$File_Path.$File_Name;  break;  Case (".mpg" OR "mpeg"):  $ContentType = "video/mpeg";  $File_Path="Music_Library/";  $File_Location=$File_Path.$File_Name;  break;  Case ".rtf":  $ContentType = "application/rtf";  $File_Path="Music_Library/";  $File_Location=$File_Path.$File_Name;  break;  Case ".pdf":  $ContentType = "application/pdf";  $File_Path="Music_Library/";  $File_Location=$File_Path.$File_Name;  break;  Case ".txt":  $ContentType = "application/txt";  $File_Path="Music_Library/";  $File_Location=$File_Path.$File_Name;  break;}@ob_end_clean();// The following code makes the download non cacheableheader("Cache-control: private");header('Pragma: private');header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");header('Content-disposition: attachment; filename='.$File_Name);header('Content-type:'.$ContentType);readfile($File_Location);exit;?>


转载于:https://my.oschina.net/startphp/blog/262353

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值