<?php 
// +++++++++++++++++++++++++++++++++++++++++++++++
//
// FileName:class_upload.php                     +
//
// 功能:文件上傳類                               +
//
// 時間:2005/11/29                               +
//
// +++++++++++++++++++++++++++++++++++++++++++++++

	class upload {
		var $agree_file_name = array('jpg','jpeg','gif','png','htm','swf'); // 充许上传的文件的文件名
		var $file_root;//目录
		var $file_name = "";//生成的文件名
		var $create_small_picture = false;//是否生成缩略图
		var $file_attribute = array(); //保存待上传文件	
		var $size = 2097152; //上传文件大小设为1M

			function upload($filename,$file_root="") {
				$this->file_root = $file_root;
				$this->file_attribute = "";
				$this->file_name = "";
				$this->up_file_attribute($filename);
				$this->check_file();	
			}
			/*生成文件属性值*/
			function up_file_attribute($filename) {
				if (is_string($filename['name'])) {
					$this->file_attribute[] = array('name'    =>$filename['name'],
													'type'    =>$filename['type'],
													'tmp_name'=>$filename['tmp_name'],
													'size'    =>$filename['size'],
													'extension' => $this->getextension($filename['name'])
													);
					
				}elseif (is_array($filename['name'])) {
					reset($filename);
					while (list($k,$v) = each($filename['name'])) {
						if ($filename['error'][$k] == 0) {
						$this->file_attribute[] = array(  'name' => $filename['name'][$k],
														  'type' => $filename['type'][$k],
														  'tmp_name' => $filename['tmp_name'][$k],
														  'size'=> $filename['size'][$k],
														  'extension'=>$this->getextension($filename['name'][$k]));
						}
					}
				}
			
			}

			/* 文件检查 */
			function check_file() {
				for ($i=0;$i<count($this->file_attribute);$i++) {
					if (in_array($this->file_attribute[$i]['extension'],$this->agree_file_name)) {
						if ($this->file_attribute[$i]['size'] < $this->size) {
							$this->upload_file($this->file_attribute[$i]['tmp_name'],$this->file_attribute[$i]['extension'],$this->file_attribute[$i]['size']);
						}
					}
				}
			
			}
			/*上传文件*/
			function upload_file($filename,$extension,$filesize) {
				$fhash = md5(uniqid(microtime()));
				$new_file_name = $this->file_root.$fhash.'.'.$extension;
				$small_file_name = $this->file_root.$fhash.'_s.'.$extension;
				move_uploaded_file($filename,$new_file_name);
				if ($filesize != filesize($new_file_name)) {
					$this->halt('Unknown Error Type!');
				}
				if (($this->create_small_picture) && (@getimagesize($new_file_name))) {
					$this->makethumb($new_file_name,$small_file_name);				
				}
				if (($this->create_small_picture) && (@getimagesize($new_file_name))) {
				$this->file_name[] = array('name'=>$new_file_name,
										   'small_name'=>$small_file_name);
				}else {
				$this->file_name[] = array('name'=>$fhash.'.'.$extension);
				}
			}
			/*生成缩略图*/
			function makethumb($srcFile,$dstFile) {
				$data = GetImageSize($srcFile,&$info);
					switch ($data[2]) {
						case 1:
						 $im = @ImageCreateFromGIF($srcFile);
					     break;
						case 2:
						 $im = @imagecreatefromjpeg($srcFile);    
						break;
					   case 3:
						 $im = @ImageCreateFromPNG($srcFile);    
						 break;
					 }
			$srcW=ImageSX($im);
			$srcH=ImageSY($im);
			$dstW=$srcW/2;
			$dstH=$srcH/2;
			if(function_exists("imagecopyresampled")){
			$newim = imagecreatetruecolor($dstW, $dstH);
			imagecopyresampled($newim, $im, 0, 0, 0, 0, $dstW,$dstH,$srcW,$srcH);
			}else{
			$newim = imagecreate($dstW, $dstH);
			imagecopyresized($newim, $im, 0, 0, 0, 0, $dstW,$dstH,$srcW,$srcH);
			}
 
			ImageJpeg($newim,$dstFile);

			}
			/*生成文件扩展名*/
			function getextension($filename) {
					return substr(strrchr($filename, "."), 1);
				}
			/*错误提示*/
			function halt($msg) {
				echo "Upload File Error:".$msg;
				exit;
			}

	}
?>