Correction status:qualified
Teacher's comments:你的命名很规范, 语义化很好
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>上传文件</title> </head> <body> <form action="demo1.php" method="post" enctype="multipart/form-data"> <!--设置上传文件大小最大为3M 这段代码一定要放到文件提交框之前--> <!-- 1M = 1024K = 1024*1024Byte=1048576Byte--> <input type="hidden" name="MAX_FILE_SIZE" value="3145728"> <input type="file" name="my_file" id=""> <!--button默认为提交按钮--> <button>上传</button> </form> </body> </html>
点击 "运行实例" 按钮查看在线实例
<?php // 自定义异常类 class CustomException extends Exception { public function errorInfo() { $str="<h3> <strong>错误代码:{$this->getCode()} --- </strong> <span style='color: red'>{$this->getMessage()}</span> </h3>"; return $str; } }
点击 "运行实例" 按钮查看在线实例
<?php require 'CustomException.php'; //引入CustomException.php class FUpload { public $flie; // 原始文件名称 protected $fileName; // 临时文件名 protected $fileTmpName; //文件类型 protected $fileType; //文件错误代码 protected $fileError; //文件大小 protected $fileSize; //文件保存路径 protected $savePath; //保存的文件名 protected $saveFileName; // 允许上传的文件后缀 protected $allowExts=['jpg','jpeg','png','gif']; public function __construct($file,$savePath='') { $this->file=$file; $this->fileName=$file['name']; $this->fileTmpName=$file['tmp_name']; $this->fileType=$file['type']; $this->fileError=$file['error']; $this->fileSize=$file['size']; $this->savePath=$savePath; if (!file_exists($savePath)){ mkdir($savePath,2019,true);//创建多级目录 } } public function __get($name) { return $this->$name; } public function __set($name, $value) { $this->$name=$value; } //文件上传 public function upload() { // 判断是否上传成功 $this->getFileError(); //判断文件后缀名是否符合要求 $extension=$this->getExtension(); $this->saveFileName=date('YmdHis',time()).mt_rand(1,99).'.'.$extension; if(is_uploaded_file($this->fileTmpName)){ if (move_uploaded_file($this->fileTmpName,$this->savePath.$this->saveFileName)){ echo '<script>alert("上传成功");history.back();</script>'; }else{ throw new CustomException('文件无法移动到指定目录, 请检查目录的写权限',103); } }else{ throw new CustomException('非法操作',102); } } //判断是否上传成功 protected function getFileError() { if($this->fileError>0){ switch ($this->fileError){ case 1: throw new CustomException('上传的文件超过了 php.ini 中 upload_max_filesize选项限制的值。',1); case 2: throw new CustomException('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。上传文件不允许超过3M',2); case 3: throw new CustomException('文件只有部分被上传。上传文件不完整',3); case 4: throw new CustomException('没有文件被上传',4); case 6: throw new CustomException('找不到临时文件夹',6); case 7: throw new CustomException('文件写入失败',7); default: throw new CustomException('未知错误',8); } } } //判断文件后缀名是否符合要求 protected function getExtension() { $myfile=explode('.',$this->fileName); $extension=array_pop($myfile); if(!in_array($extension,$this->allowExts)){ throw new CustomException('不允许上传'.$extension.'文件类型',101); } return $extension; } }
点击 "运行实例" 按钮查看在线实例
<?php require 'FUpload.php'; try{ $obj=new FUpload($_FILES['my_file'],'uploads/img/'); $obj->upload(); }catch (CustomException $e){ echo $e->errorInfo(); }
点击 "运行实例" 按钮查看在线实例