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方法裏面能夠寫一些參數提供的方法,地址啊,大小啊,原名啊之類的,自由發揮便可測試

相關文章
相關標籤/搜索