写一个自定义异常类来处理上传过程以及各种错误
新建一个index.html文件,用于文件的上传页面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="upload.php" method="POST" enctype="multipart/form-data"> <input type="file" name="my_file" id=""> <button>上传</button> </form> </body> </html>
此处注意:提交类型必需选择POST方式。
新建一个upload.php文件处理文件的上传。
<?php namespace _001; use Exception; class UpException extends Exception{ public function __construct($message = "", $code = 0) { parent::__construct($message, $code); } public function errorInfo(){ return <<< "ERROR" <h2> {$this->getCode()}:{$this->getMessage()} </h2> ERROR; } } try{ //第一步:配置上传信息 //文件原始名称 $fileName = $_FILES['my_file']['name']; //文件临时名称 $tempFile = $_FILES['my_file']['tmp_name']; //允许上传的文件类型 $fileType = ['jpg','png','jpeg','gif']; //允许上传的文件大小 $fileSize = 3145728; //上传到服务器的指定目录 $filePath = '/Uploads/'; //第二步:查看是否上传成功 $uploadError = $_FILES['my_file']['error']; if($uploadError > 0){ switch ($uploadError){ case 1:throw new UpException('超过php.in配置中文件上传的最大长度',101); case 2:throw new UpException('超过允许上传文件的最大长度',102); case 3:throw new UpException('文件只有部分被上传',103); case 4:throw new UpException('没有文件被上传',104); case 6:throw new UpException('找不到临时上传文件夹',106); case 7:throw new UpException('文件写入失败',107); } } //第三步:检查文件的扩展名是否正确 $extension = explode('.',$fileName)[1]; if(!in_array($extension,$fileType)){ throw new UpException('上传的文件类型:'.$extension.'不被允许',110); } //第四步:重命名文件名称防止重名 $fileName=date('YmdHis',time()).md5(round(1,99)).'.'.$extension; //第五步:把文件储存到指定位置 if(is_uploaded_file($tempFile)){ if(move_uploaded_file($tempFile,__DIR__.$filePath.$fileName)){ echo '<script>alert("上传成功");history.back();</script>'; }else{ throw new UpException('文件无法移动到指定的目录,请检查目录的权限',111); } }else{ throw new UpException('非法操作',444); } } catch (UpException $e){ echo $e->errorInfo(); }
2. (选做) 写一个与指定数据表绑定的类, 实现基本的模型功能,例如查询, 新增, 更新,删除等操作
<?php namespace _002; use PDO; class Staff { private $staff_id; private $name; private $age; private $sex; private $position; private $mobile; private $hiredate; //属性重载 public function __get($name) { return $this->$name; } public function __set($name, $value) { $this->$name = $value; } //构造方法 public function __construct() { $this->hiredate = date('Y/m/d', $this->hiredate); $this->sex = $this->sex ? '男' : '女'; } } $pdo=new PDO('mysql:dbname=staff','root','root'); //查询数据 $stmt=$pdo->prepare('SELECT * FROM `staff`'); //数据表和类进行绑定 $stmt->setFetchMode(PDO::FETCH_CLASS,Staff::class); $stmt->execute(); while($staff = $stmt->fetch()){ echo "<li>{$staff->staff_id}: {$staff->name}--{$staff->sex}--{$staff->hiredate}</li>"; } //增加数据 $stmt=$pdo->prepare("INSERT INTO `staff` (name,age,sex,position,mobile,hiredate) VALUES ('张三',18,1,'厕所管理员','13908145589','1420041450')"); //数据表和类进行绑定 $stmt->setFetchMode(PDO::FETCH_CLASS,Staff::class); $stmt->execute(); echo '新增'.$stmt->rowcount().'条数据'; //修改数据 $stmt=$pdo->prepare('UPDATE `staff` SET sex=0 WHERE staff_id=1'); //数据表和类进行绑定 $stmt->setFetchMode(PDO::FETCH_CLASS,Staff::class); $stmt->execute(); echo '更新'.$stmt->rowcount().'条数据'; //删除数据 $stmt=$pdo->prepare('DELETE FROM `staff` WHERE staff_id=21'); //数据表和类进行绑定 $stmt->setFetchMode(PDO::FETCH_CLASS,Staff::class); $stmt->execute(); echo '删除'.$stmt->rowcount().'条数据';