批改状态:合格
老师批语:文件的上传是比较麻烦了, 相反 下载却很容易实现, 想一想原理是什么?
很简单, 只要是浏览器不能直接解析的文档, 都会提示用户是否下载, 是不是so eaay?
第一个知识点 异常处理类Exception,用于处理PHP中的各种异常,也可以重写这个类来进行异常信息样式输出。
第二个知识点 php文件上传处理
第三个知识点 php模型类与数据表的绑定。
作业:写一个自定义异常类来处理上传过程以及各种错误
<?php
namespace __1012;
use Exception;
//重写一个异常处理类,用来自定义异常输出样式
class userException extends Exception{
public function __construct($message = "", $code = 0)
{
parent::__construct($message, $code);
}
public function error(){
return '<h3 style="color:red;"><span style="font-weight: bold">错误代码:'.$this->code.',</span>'.$this->message.'</h3>';
}
}
try{
//设置数组用来保存可上传文件类型后缀
$allowUp=['jpg','jpeg','png','gif'];
$fileName=$_FILES['file']['name'];
//将上传的文件名用explode()函数处理成以点为分割的数组
$tempList=explode('.',$fileName);
//为防止文件名中有多个点,使用end()函数取出数组中最后一个元素做为文件的类型处理
$type=end($tempList);
//设置上传文件的大小
$fileSize=204800;
//如果文件名为空,返回错误信息
if(empty($_FILES['file']['name'])){
throw new userException('没有选择文件',111);
}
//如果文件类型不在可上传文件数组中,返回错误信息
if(!in_array($type,$allowUp)){
throw new userException('不允许上传'.$type.'文件,只能上传图片类型',101);
}
//如果上传文件的大小超出,返回错误信息
if($_FILES['file']['size']>$fileSize){
throw new userException('文件大小超出限制,只能上传200KB内的图片',102);
}
//文件上传的错误代码,使用switch来循环,错误代码是查看的手册
if($_FILES['file']['error']>0){
switch ($_FILES['file']['error']){
case 1:
throw new userException('文件大小超出限制',103);
break;
case 2:
throw new userException('文件大小超出限制,只能上传200KB内的图片',102);
break;
case 3:
throw new userException('只有部分文件被上传',104);
break;
case 4:
throw new userException('没有文件被上传',105);
break;
case 6:
throw new userException('找不到临时文件夹',106);
break;
case 7:
throw new userException('文件写入失败',107);
break;
default:
throw new userException('未知错误',108);
}
}
//创建一个文件夹命名为时间格式,方便整理
$path='upload/'.date('Ymd');
//判断文件夹是否存在,不存在创建文件夹
if(!is_dir($path)){
$path=mkdir($path,777,true);
}
//重命名文件,取时间格式加md5加密1到99随机数,防止文件重名。
$fileUploadName=date('YmdHis',time()).md5(mt_rand(1,99)).'.'. $type;
//根据是否存在临时文件来判断文件是否上传成功
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
//移动文件到创建的时间格式文件夹内,并重新命名
if(move_uploaded_file($_FILES['file']['tmp_name'],$path.'/'.$fileUploadName))
{
echo '<script>alert("文件上传成功");history.back();</script>';
}else{
throw new userException('文件上传失败,可能是目录权限有问题',109);
}
}else{
throw new userException('非法操作',110);
}
}catch (userException $e){
echo $e->error();
}点击 "运行实例" 按钮查看在线实例
结果演示

作业:写一个与指定数据表绑定的类, 实现基本的模型功能,例如查询, 新增, 更新,删除等操作。
根据之前的数据库链式操作查询电影输出,没有足够的时间,先写一个查询交作业,再来试试其他的操作。主要是我不知道该怎么
<?php
namespace __1012;
class Select{
public $pdo=null;
public $table;
public $field='*';
public $where;
public $limit;
public function __construct($pdo)
{
$this->pdo=$pdo;
}
public function table($tableName){
$this->table=$tableName;
return $this;
}
public function field($field='*'){
$this->field=empty($field) ? $field : $field;
return $this;
}
public function where($where=''){
$this->where=empty($where) ? $where:' WHERE '.$where;
return $this;
}
public function limit($limit=''){
$this->limit=empty($limit) ? $limit :' LIMIT '.$limit;
return $this;
}
public function select(){
$sql='SELECT '
. $this->field
.' FROM '
.$this->table
.$this->where
.$this->limit;
$stmt=$this->pdo->prepare($sql);
$stmt->execute();
$stmt->setFetchMode(\PDO::FETCH_CLASS,movies::class);
return $stmt->fetchAll();
}
}
//数据库连接类
class Db{
protected static $pdo=null;
public static function connect(){
self::$pdo=new \PDO('mysql:host=127.0.0.1;dbname=www.tao.io','root','root');
}
public static function __callStatic($name, $arguments)
{
self::connect();
$query=new Select(self::$pdo);
return call_user_func_array([$query,$name],$arguments);
}
}
//用来绑定数据库的类
class movies{
private $mov_id;
private $name;
private $image;
private $detail;
private $cate_id;
public function __get($name){
return $this->$name;
}
public function __construct()
{
$this->image='./static/images/'.$this->image;
}
public function __set($name,$value){
$this->$name=$value;
}
}
$staffs = Db::table('movies')->field()->where()->limit( )->select();
foreach ($staffs as $v){
echo "<h3>{$v->mov_id}、{$v->name}</h3>";
echo "<div><img src='".$v->image."' width='150'><p>".$v->detail."</p></div>";
}点击 "运行实例" 按钮查看在线实例

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号