php文件上传类

博文里有另外一个图文混合上传方法,不过此次写的比以前的更严谨规范一些。也有大量的注释。php

能够配合html本身先配置一下试试,有问题能够联系我探讨。html

另外,,,写的时候必定要细心。。此次我就是取反的地方马虎多写了一个叹号找了各类定位找了半天问题。。另外就是状况容许的条件下仍是写一块测试一块比较好,这个我写的时候是直接写完再测,测得时候出问题,问题定位效率就比较低了。数组

先上html部分函数

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>文件上传</title>
</head>
<body>
	<form action="Upload.php" method="post" enctype="multipart/form-data">
		<input type="file" name="fm"><br />
		<input type="submit" value="上传"><br />
	</form>
</body>
</html>

代码部分post

<?php 

$up = new Upload();
$up->uploadFile('fm');

var_dump($up->errorNumber);
var_dump($up->errorInfo);

class Upload
{
	protected $path = './upload/';//文件上传保存路径
	protected $allowSuffix = ['jpg','jpeg','gif','png'];
	protected $allowMime = ['image/jpeg','image/jpg','image/gif','image/png'];
	protected $maxSize = 2048000;
	protected $isRandName = true;//是否取随机名称
	protected $prefix = 'up_';//前缀

	protected $errorNumber;//错误号码
	protected $errorInfo;//错误信息

	protected $oldName;//源文件名
	protected $suffix;//后缀
	protected $size;//大小
	protected $mime;//类型
	protected $tmpName;//临时文件名

	protected $newName;//新名字


	public function __construct($arr = [])
	{
		foreach ($arr as $key => $value) {
			$this->setOption($key,$value);
		}
	}

	protected function setOption($key,$value)
	{//判断$key是否是成员属性,如是则设置
		$keys = array_keys(get_class_vars(__CLASS__));//得到数组,键为成员属性名,值为成员属性值,在获得全部的键

		if (in_array($key, $keys)) {
			$this->$key = $value;//若是$key是成员属性,那就设置
		}
	}

	/*
	*有没有设置路径
	*判断路径是否存在,是否可写
	*$_FILES里面的error信息是否为0
	*文件大小,类型,后缀是否符合要求
	*是否新取名,得到新名
	*是否上传文件并移动
	*/
	public function uploadFile($key)
	{//文件上传函数
		if (empty($this->path)) {
			$this->setOption('errorNumber',-1);
			return false;
		}
		if (!$this->check()) {
			$this->setOption('errorNumber',-2);
			return false;
		}
		$error = $_FILES[$key]['error'];
		if ($error) {
			$this->setOption('errorNumber',$error);
			return false;
		}else{
			//提取文件信息并保存
			$this->getFileInfo($key);
		}

		if (!$this->checkSize() || !$this->checkMime() || !$this->checkSuffix()) {
			return false;
		}
		//新名称
		$this->newName = $this->createNewName();

		//判断是否上传移动文件
		if (is_uploaded_file($this->tmpName)) {
			if (move_uploaded_file($this->tmpName,$this->path.$this->newName)) {
				return $this->path.$this->newName;
			}else{
				$this->setOption('errorNumber',-7);
				return false;
			}
		}else{
			$this->setOption('errorNumber',-6);
			return false;
		}
	}


	protected function createNewName()
	{
		if ($this->isRandName) {
			$name = $this->prefix.uniqid().'.'.$this->suffix;
		}else{
			$name = $this->prefix.$this->oldName;
		}
		return $name;
	}

	protected function check()
	{
		//不存在或非目录则建立
		if (!file_exists($this->path)||!is_dir($this->path)) {
			return mkdir($this->path,0777,true);
		}
		//判断是否可写
		if (!is_writeable($this->path)) {
			return chmod($this->path,0777);
		}
		return true;
	}

	protected function getFileInfo($key)
	{
		//获取文件名
		$this->oldName = $_FILES[$key]['name'];
		$this->mime = $_FILES[$key]['type'];
		$this->tmpName = $_FILES[$key]['tmp_name'];
		$this->size = $_FILES[$key]['size'];
		$this->suffix = pathinfo($this->oldName)['extension'];
	}

	protected function checkSize()
	{
		if ($this->size > $this->maxSize) {
			$this->setOption('errorNumber',-3);
			return false;
		}
		return true;
	}

	protected function checkMime()
	{
		if (!in_array($this->mime,$this->allowMime)) {
			$this->setOption('errorNumber',-4);
			return false;
		}
		return true;
	}

	protected function checkSuffix()
	{
		if (!in_array($this->suffix,$this->allowSuffix)) {
			$this->setOption('errorNumber',-5);
			return false;
		}
		return true;
	}

	public function __get($name)
	{
		if ($name == 'errorNumber') {
			return $this->errorNumber;
		}elseif ($name == 'errorInfo') {
			return $this->getErrorInfo();
		}
	}

	protected function getErrorInfo()
	{
		switch ($this->errorNumber) {
			case -1:
				$str = '文件路径没有设置';
				break;
			case -2:
				$str = '文件路径不是目录';
				break;
			case -3:
				$str = '过大';
				break;
			case -4:
				$str = '类型错误';
				break;
			case -5:
				$str = '-5后缀错误';
				break;
			case -6:
				$str = '-6不是上传文件';
				break;
			case -7:
				$str = '-7移动失败';
				break;
			case 1:
				$str = 'ini错误';
				break;
			case 2:
				$str = '超出html';
				break;
			case 3:
				$str = '部分上传';
				break;
			case 4:
				$str = '找不到临时文件';
				break;
			case 5:
				$str = '保存失败';
				break;
			return $str;
		}
	}
}


?>

忘了get方法里面能够写一些参数提供的方法,地址啊,大小啊,原名啊之类的,自由发挥便可测试

相关文章
相关标签/搜索