<?php class upload { private $allow = ['image']; // 单文件上传 public function single( ) { // error $key = key($_FILES); if(empty($key)){ return '您的文件过大, 请从新上传'; } $error = $_FILES[$key]['error']; if($error > 0){ switch($error){ case '1': return '您的文件过大, 请从新上传'; case '2': return '您的文件过大, 请从新上传'; case '3': return '请检查您的网络'; case '4': return '请上传您的文件'; case '6': return '服务器繁忙'; case '7': return '服务器繁忙'; } } // post if(!is_uploaded_file( $_FILES[$key]['tmp_name'])){ return '非法上传'; } // 文件类型 $type = strtok($_FILES[$key]['type'], '/'); if( !in_array($type, $this->allow) ){ return '文件类型不符'; } // 新的文件名 // 20180208xxxxxx.jpg $suffix = strrchr($_FILES[$key]['name'], '.'); $fileName = date('Ymd').uniqid().$suffix; // 新的目录 // upload/2018/02/08/ $dir = UPLOAD.date('/Y/m/d/'); if( !file_exists($dir) ){ mkdir($dir, 0777, true); } // 移动临时文件 if( move_uploaded_file($_FILES[$key]['tmp_name'], $dir.$fileName)){ $file[] = $fileName; return $file; } return '上传失败'; } public function fileType($condition = '') { if(!is_array($condition)){ return false; }else{ $this->allow = $condition; } return $this; } }