PHP文件上传类

<?php
/**
 	PHP 多文件上传类
 */
class Upload{

	//自定义类型
	private $type;

	//自定义大小
	private $size;

	//自定义路径
	private $path;

	//自定义表单name值
	private $file;

	//源文件临时存储位置
	private $tmp_name;

	//源文件文件名
	private $orig_file;

	//源文件大小
	private $orig_size;

	//源文件类型
	private $orig_type;

	//上传后新文件名
	private $new_file;

	//构造方法初始化
	public function __construct($type, $size, $path, $file){

		$this->type = $type;
		$this->size = $size;
		$this->path = $path;
		$this->file = $file;

	}

	//执行文件上传
	public function runing(){
		$count = count($_FILES[$this->file]["name"]);
		for($i=0; $i<$count; $i++){
			$this->tmp_name = $_FILES[$this->file]["tmp_name"][$i];
			$this->orig_file = $_FILES[$this->file]["name"][$i];
			$this->orig_size = $_FILES[$this->file]["size"][$i];
			$this->orig_type = strtolower(substr($this->orig_file, strrpos($this->orig_file, ".")+1));
			$this->new_file = $this->path.date("Y-m-d-H-i-s").rand(0,999).".".$this->orig_type;
			
			if($this->checktype()){
				if($this->checksize()){
					$this->fileupload();

				}else{
					echo "<meta charset='utf-8'>该文件大小错误:".$this->orig_file;
				}
			}else{
				echo "<meta charset='utf-8'>该文件类型错误:".$this->orig_file;
				return false;
			}
			
		}
	}

	//检查文件的类型
	private function checktype(){
		if(in_array($this->orig_type, $this->type)){
			return true;
		}else{
			return false;
		}
	}

	//检查文件大小
	private function checksize(){
		
		if($this->orig_size <= $this->size){
			return true;
		}else{
			return false;
		}
	}

	//执行上传
	private function fileupload(){
		
		if(is_uploaded_file($this->tmp_name) && isset($_POST["submit"])){
			$file = move_uploaded_file($this->tmp_name, $this->new_file);
		}else{
			return false;
		}
		if($file){
			echo $this->orig_file."<meta charset='utf-8'>上传成功!";
		}else{
			echo $this->orig_file."<meta charset='utf-8'>上传失败!";
		}
	}


}

?>

  

原文地址:https://www.cnblogs.com/chenshuo/p/3670103.html