Correction status:qualified
Teacher's comments:案例不错, 结合之前所学
知识点:
count() 统计数组总数量;
in_array() 搜索数组中是否存在指定的值;
array_shift() 删除数组中的第一个元素(red),并返回被删除元素的值;
is_numeric() 判断是否是数值类型;
<?php //自定义异常类 class CalException extends Exception { public function __construct($message = "", $code = 0) { parent::__construct($message, $code ); } public function errorInfo() { return <<<ERROR <h2> <strong>{$this->getCode()}</strong> <sapn style="color: red">{$this->getMessage()}</span> </h2> ERROR; } }
点击 "运行实例" 按钮查看在线实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>上传文件</title> </head> <body> <form action="demo6.php" method="POST" enctype="multipart/form-data"> //限制上传文件大小 <input type="hidden" name="MAX_FILE_SIZE" value="3145728"> <input type="file" name="photo" > <button>上传</button> </form> </body> </html>
点击 "运行实例" 按钮查看在线实例
<?php namespace demo6; use CalException; require 'CalException.php'; try { //允许上传文件的类型 $fileType = ['jpg','jpeg', 'png', 'gif']; //上传文件的大小 $fileSize = 3145728; //上传文件的路径 $filePath = '/images/'; //原始文件名称 $fileName = $_FILES['photo']['name']; //临时文件名称 $tmpFile = $_FILES['photo']['tmp_name']; //判断是否上传成功 $uploadError = $_FILES['photo']['error']; if ($uploadError > 0) { switch ($uploadError) { case 1: case 2: throw new CalException('上传文件不允许超过3M', 101); case 3: throw new CalException('上传文件不完整', 102); case 4: throw new CalException('没有文件被上传', 103); default: throw new CalException('未知错误', 104); } } //判断扩展名是否正确? $extension = explode('.', $fileName)[1]; if (!in_array($extension, $fileType)) { throw new CalException('不允许上传'.$extension.'文件类型',201); } //防止同名文件相互覆盖 $fileName = date('YmdHis',time()).md5(mt_rand(1,99)).'.'.$extension; // 上传文件 //检测是否通过POST上传的 if (is_uploaded_file($tmpFile)){ if(move_uploaded_file($tmpFile,__DIR__.$filePath.$fileName)){ echo '<script>alert("上传成功");history.back();</script>'; }else{ throw new CalException('文件无法移动到指定目录',301); } }else{ throw new CalException('非法操作',302); } }catch(CalException $e){ echo $e->errorInfo(); }
点击 "运行实例" 按钮查看在线实例
<?php namespace _0808; use PDO; class Movies { private $mov_id; private $name; private $image; private $detail; private $cate_id; //属性重载 public function __get($name) { return $this->$name; } public function __set($name, $value) { return $this->$name = $value; } //构造方法 public function __construct() { switch ($this->cate_id) { case 1: $this->cate_id = '国产好剧'; break; case 2: $this->cate_id = '欧美猛片'; break; case 3: $this->cate_id = '日韩新片'; break; } } } $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); $stmt = $pdo->prepare('SELECT * FROM `movies`'); //为语句设置默认的获取模式 类名模式 $stmt->setFetchMode(PDO::FETCH_CLASS,Movies::class); $stmt->execute(); //var_dump($stmt->fetch()); //循环 while($movies = $stmt->fetch()){ echo "<li>{$movies->mov_id}:{$movies->name}--{$movies->image}--{$movies->cate_id}</li>"; }
点击 "运行实例" 按钮查看在线实例