class Upload
{
//保存路徑
protected $savePath = './';
//日期目錄
protected $datePath = true;
//隨機(jī)名字
protected $randName = true;
//默認(rèn)后綴
protected $extension = 'png';
//允許MIME
protected $mimes = ['image/png','image/jpeg','image/gif'];
//允許后綴
protected $suffixes = ['png','jpg','jpeg','gif'];
//最大尺寸
protected $maxSize = 2000000;
//錯(cuò)誤代碼
protected $errorNumber = 0;
//錯(cuò)誤信息
protected $errorMessage = '上傳成功';
//上傳信息
protected $uploadInfo;
//文件名稱
protected $pathName;
public function __construct($options=null)
{
$this->setOption($options);
}
著作權(quán)歸作者所有堪澎。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán)颠猴,非商業(yè)轉(zhuǎn)載請(qǐng)注明出處⊥互聯(lián)網(wǎng)+時(shí)代渣玲,時(shí)刻要保持學(xué)習(xí)逗概,攜手千鋒PHP,Dream?It?Possible。
public function setOption($options)
{
if (is_array($options)) {
$keys = get_class_vars(__CLASS__);
foreach ($options as $key => $value) {
//在屬性列表中才設(shè)置屬性
if (in_array($key, $keys)) {
$this->$key = $value;
}
}
}
}
public function uploadFile($field)
{
//1忘衍、檢查保存路徑
if (!$this->checkSavePath()) {
return false;
}
//2逾苫、檢查上傳信息
if (!$this->checkUploadInfo($field)) {
return false;
}
//3、檢查系統(tǒng)錯(cuò)誤
if (!$this->checkUploadError()) {
return false;
}
//4枚钓、檢查自定義錯(cuò)誤
if (!$this->checkAllowOption()) {
return false;
}
//5铅搓、檢車是否是上傳文件
if (!$this->checkUploadFile()) {
return false;
}
//6、拼接路徑名
$this->joinPathName();
//7搀捷、移動(dòng)上傳文件
if (!$this->moveUploadFile()) {
return false;
}
return true;
}
protected function checkSavePath()
{
if (!is_dir($this->savePath)) {
$this->errorNumber = -1;
$this->errorMessage = '保存路徑不存在';
return false;
}
if (!is_writable($this->savePath)) {
$this->errorNumber = -2;
$this->errorMessage = '保存路徑不可寫';
return false;
}
$this->savePath = rtrim($this->savePath,'/') . '/';
return true;
}
protected function checkUploadInfo($field)
{
if (empty($_FILES[$field])) {
$this->errorNumber = -3;
$this->errorMessage = '沒有'.$field.'相關(guān)上傳信息';
return false;
}
$this->uploadInfo = $_FILES[$field];
return true;
}
protected function checkUploadError()
{
if ($this->uploadInfo['error'] == 0) {
return true;
}
switch ($this->uploadInfo['error']) {
case UPLOAD_ERR_INI_SIZE:
$this->errorMessage = '超出了配置文件中設(shè)定文件大小';
break;
case UPLOAD_ERR_FORM_SIZE:
$this->errorMessage = '超出了MAX_FILE_SIZE的大小';
break;
case UPLOAD_ERR_PARTIAL:
$this->errorMessage = '只有部分文件被上傳';
break;
case UPLOAD_ERR_NO_FILE:
$this->errorMessage = '沒有文件被上傳';
break;
case UPLOAD_ERR_NO_TMP_DIR:
$this->errorMessage = '沒有找到臨時(shí)文件夾';
break;
case UPLOAD_ERR_CANT_WRITE:
$this->errorMessage = '文件寫入失敗';
break;
default:
$this->errorMessage = '未知錯(cuò)誤';
break;
}
$this->errorNumber = $this->uploadInfo['error'];
return false;
}
protected function checkAllowOption()
{
if (!in_array($this->uploadInfo['type'],$this->mimes)) {
$this->errorNumber = -4;
$this->errorMessage = '不允許的MIME:'.$this->uploadInfo['type'];
return false;
}
if (!in_array($this->extension,$this->suffixes)) {
$this->errorNumber = -5;
$this->errorMessage = '不允許的后綴:'.$this->extension;
return false;
}
if ($this->uploadInfo['size'] > $this->maxSize) {
$this->errorNumber = -6;
$this->errorMessage = '超出了規(guī)定大小:'.$this->maxSize.'字節(jié)';
return false;
}
return true;
}
protected function checkUploadFile()
{
if (!is_uploaded_file($this->uploadInfo['tmp_name'])) {
$this->errorNumber = -7;
$this->errorMessage = '不是上傳文件';
return false;
}
return true;
}
protected function joinPathName()
{
//路徑
$this->pathName = $this->savePath;
if ($this->datePath) {
$this->pathName .= date('Y/m/d/');
if (!file_exists($this->pathName)) {
mkdir($this->pathName,0777,true);
}
}
//名字
if ($this->randName) {
$this->pathName .= uniqid();
} else {
$info = pathinfo($this->uploadInfo['name']);
$this->pathName .= $info['filename'];
}
//后綴
$this->pathName .= '.' . $this->extension;
}
protected function moveUploadFile()
{
if (move_uploaded_file($this->uploadInfo['tmp_name'], $this->pathName)) {
return true;
}
$this->errorNumber = -8;
$this->errorMessage = '上傳文件保存';
return false;
}
}
更多PHP相關(guān)技術(shù)請(qǐng)搜索千鋒PHP星掰,做真實(shí)的自己,用良心做教育