php 文件上传类

class IcoUpLoad{
	

/* 以下为默认值 */
private	$destination_folder = "../images/";
private $destination_url = "../images/";	// 插入数据库的地址
private	$max_ico_size = 1048576; 			//上传文件大小限制 5MB , 单位BYTE
private	$path_default_name = "default_ico.jpg";		//上传失败,或者没有选择图片时 默认的文件
private	$path_default_iocn = "../default_ico.jpg"; 	// 没有上传图片 默认图片
private $if_rand_filename = 0;  // 随机文件名,当批量上传时使用的time()可能出现同名文件,
								// 因此而使用,当值为0时则不启用该功能,随机数的范围为[0 , $if_rand_filename]
private $if_microtime = true;	// 是否使用毫秒时间戳

private $ico_name; 		// 文件原名
private $ico_tmpname;	// 临时文件名
private $ico_info; 		// 完整的路径信息
private $ico_rename;	// 新名字 不带扩展名
private $ico_extension; // 文件的扩展名
private $ico_type;		// 文件类型 (MIME值)
private $ico_size;		// 文件大小
private $ico_path;		// 文件移至的目标路径
private $ico_url;		// 文件的url
private $ico_width;		// = $size[0]; 
private $ico_height;	// = $size[1]; 
private $error;			// 文件移至过程中的错误
private	$uptypes = array(
			'image/jpg',
			'image/jpeg',
			'image/png',
			'image/pjpeg',
			'image/gif',
			'image/bmp',
			'image/x-png'
		);

function __construct($ico){
	
	$this->max_ico_size *= 5;
	
	if (!empty($ico['tmp_name'])){
		$size = getimagesize($ico['tmp_name']);
		$this->ico_width = $size[0];
		$this->ico_height = $size[1];
	}else
		$this->ico_height = $this->ico_width = "default";
	$this->ico_tmpname = $ico['tmp_name'];
	$this->ico_name = $ico['name']; // 原名
	if ($this->if_microtime ){
		$this->ico_rename = $this->GetMicroTime();
	}else{
		$this->ico_rename = time();
	}
	if ($this->if_rand_filename){
		$this->ico_rename .= rand();
	}
	$this->ico_size = $ico['size'];
	$this->ico_type = $ico['type'];
	$this->ico_info = pathinfo($ico['name']);
	$this->ico_extension = $this->ico_info['extension'];
	$this->ico_path = $this->destination_folder.$this->ico_rename.".".$this->ico_extension;
	$this->ico_url = $this->destination_url.$this->GetIcoName();
//	$this->ico_url = $this->ico_path;
	$this->Upload();
}
	
	
public	function GetIcoPath(){	return $this->ico_path;	} 	
public	function GetError(){	return $this->error;	}
public	function GetIcoName(){
			if ($this->ico_rename)
				return $this->ico_rename.".".$this->ico_extension;	
			else
				return $this->path_default_name;
		}
private	function GetIcoOldName(){	return $this->ico_name;	}
private	function GetIcoSize(){	return $this->ico_size." B";	}
private	function GetIcoType(){	return $this->ico_type;	}
private	function GetIcoWidth(){	return $this->ico_width;	}
private	function GetIcoHeight(){	return $this->ico_height;	}
private function GetMicroTime(){
			$Mbit = 1000000;
			list($usec, $sec) = explode(" ",microtime()); // list 按下标顺序把右边数组中的值依次赋给左边的变量
			return ((float)$sec . (float)$usec * $Mbit) ;
		}


private
	function Upload(){
		$this->error = NULL;
		if ( !is_uploaded_file($this->ico_tmpname) ){
			$this->error = "no_ico";
			$this->ico_path = $this->path_default_iocn;
			$this->ico_rename = NULL;
		}else{
			if( empty($this->error) && !file_exists($this->destination_folder)){//创建目录
				mkdir($this->destination_folder);
			}
			if( empty($this->error) && !in_array($this->ico_type, $this->uptypes)){//检查文件类型
				$this->error = "error_type";
			}
			if( empty($this->error) && ($this->ico_size) > ($this->max_ico_size) ){//检查文件大小
				$this->error = "oversize";
			}
			if( empty($this->error) && file_exists($this->ico_path)){//检查同名文件	
				$this->error = "exsit_same_iconame";
			}
			
			if( empty($this->error) && move_uploaded_file ($this->ico_tmpname, $this->ico_path) == false){//移动文件	
				$this->error = "move_error";
			}
		}// End of if else
	}// End of function
	
	
public 
	function Debug(){
		
			echo "ico tmpname: ".$this->ico_tmpname."<br>";
			echo "ico path: ".$this->GetIcoPath()."<br>";
			echo "error: <font color='red'>".$this->GetError()."</font><br>";
			echo "ico oldname: ".$this->GetIcoOldName()."<br>";
			echo "ico name: ".$this->GetIcoName()."<br>";
			echo "ico size: ".$this->GetIcoSize()."<br>";
			echo "ico type: ".$this->GetIcoType()."<br>";
			echo "ico width: ".$this->GetIcoWidth()."px<br>";
			echo "ico height: ".$this->GetIcoHeight()."px<br>";
			echo "<img width=\"$this->GetIcoWidth()\" hegith=\"$this->GetIcoHeight()\" src=\"$this->ico_url\"  /><br>";
	
	} // End of function
}; // End of class 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值