<?php/** * Created by PhpStorm. * User: john shuxian * Date: 2018/6/22 * Time: 16:58 */error_reporting(E_ALL);class uploadsController{ protected $ext;#如png,jpg protected $max_file_size; protected $save_path; protected $error_msg = array(); protected $files;#表单name名 protected $realname;#文件真实名 protected $type;#文件类型 如image flash protected $allow_ext = array(); public function __construct($files) { //定义允许上传的文件扩展名 $this->allow_ext = array( 'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'), 'flash' => array('swf', 'flv'), 'media' => array('swf', 'flv', 'mp3', 'wav', 'wm a', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'), 'file' => array('doc', 'docx', 'xls', 'xlsx', 'pp t', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2','sql'), ); $this->max_file_size = 90*1024*1024; $this->save_path = "./uploads/"; $this->files = $files; } public function uploads() { if($this->checkError($_FILES[$this->files])){ if(!file_exists($this->save_path)){ //文件夹不存在时,自动创建目录 mkdir($this->save_path); } $new_name = $this->randomName($this->ext); if(move_uploaded_file($_FILES[$this->files]['tmp_name'],$new_name) === false){ array_push($this->error_msg,"文件上传失败,请重试"); $this->dd($this->error_msg); }else{ return json_encode(array('type'=>"success",'new_file'=>$new_name)); } } } /** * 返回随机文件名 * @param $ext * @return string */ public function randomName($ext){ return $this->save_path.date("YmdHis")."_".rand(1000,9000).".".$ext; } /** * 检查错误 * @param $files_post * @return bool */ public function checkError($files_post) { if(empty($files_post)){ array_push($this->error_msg,"未知错误"); $this->dd($this->error_msg,false); return false; }else{ $error = $files_post['error']; if($error!=0){ switch ($error){ case 1: array_push($this->error_msg,"上传文件超过系统设置"); break; case 2: array_push($this->error_msg,"上传的文件大小超过了HTML表单设置的MAX_FILE_SIZE选项设置的值"); break; case 3: array_push($this->error_msg,"文件只有部分被上传"); break; case 4: array_push($this->error_msg,"没有文件被上传"); break; case 6: array_push($this->error_msg,"找不到临时文件夹"); break; case 7: array_push($this->error_msg,"文件写入失败"); break; } return false; }else{ $this->realname = $files_post['name']; $this->ext =explode('.',$this->realname)[1]; $this->type = explode('/',$files_post['type'])[0]; if(isset($this->allow_ext[$this->type])&&in_array($this->ext,$this->allow_ext[$this->type])){ if($files_post['size']>$this->max_file_size){ array_push($this->error_msg,"文件大小超出限制,请选择小于".$this->max_file_size/(1024*1024)."M的文件"); $this->dd($this->error_msg); } return true; }else{ array_push($this->error_msg,"文件类型不支持"); $this->dd($this->error_msg); return false; } } } } public function dd(array $values,$echo = true){ $log_name = "./".date("Y-m-d").".log"; if(!$echo){ $file = fopen($log_name,'w+'); if(!is_writable($log_name)){ array_push($this->error_msg,"日志文件不可写,请检查文件权限"); var_dump($this->error_msg); die(); } foreach ($values as $v){ $vv = $v; if(!is_string($v) && !is_numeric($v)){ $vv = json_encode($v); } $vv.=PHP_EOL; fwrite($file,$vv); } fclose($file); }else{ var_dump($values); die(); } }}$uploads = new uploadsController('pic');$uploads->uploads();