找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 672|回复: 0

异常的实际使用

[复制链接]

296

主题

38

回帖

1272

积分

管理员

积分
1272
发表于 2021-4-22 12:50:44 | 显示全部楼层 |阅读模式
1.结合异常的使用
  1. <?php

  2. // 文件上传类
  3. class Uploads
  4. {
  5.         // 文件上传的信息
  6.         protected $files = [];

  7.         // 上传配置
  8.         protected $config = [
  9.                 // 默认限定大小,为0表示不限制
  10.                 'allowSize' => 0,
  11.                 // 默认限定类型,为空表示不限制
  12.                 'allowType' => [],
  13.                 // 上传文件保存的目录
  14.                 'uploadDir' => './upload/',
  15.         ];

  16.         // 随机名字的存储
  17.         protected $fileName = '';

  18.         // 文件的拓展名
  19.         protected $ext = '';

  20.         // 构造方法
  21.         public function __construct(string $field, array $config = [])
  22.         {
  23.                 // 保证存在字段
  24.                 if (!isset($_FILES[$field])) {
  25.                         throw new \Exception('上传文件的信息不存在!');
  26.                 }

  27.                 // 获取上传文件的信息
  28.                 $this->files = $_FILES[$field];

  29.                 // 合并配置
  30.                 $this->config = array_merge($this->config, $config);

  31.                 // 创建保存目录
  32.                 $this->makedir();
  33.         }

  34.         // 创建保存目录
  35.         protected function makeDir()
  36.         {
  37.                 // 处理保存文件的路径
  38.                 $this->config['uploadDir'] = rtrim($this->config['uploadDir'], '/') . '/';

  39.                 // 确保这个路径存在
  40.                 if (!file_exists($this->config['uploadDir'])) {
  41.                         if (!mkdir($this->config['uploadDir'], 0777, true)) {
  42.                                 throw new \Exception('创建保存目录失败,请检查权限问题!');
  43.                         }
  44.                 }
  45.         }

  46.         // 错误判断
  47.         protected function checkError()
  48.         {
  49.                 // 1. 确保文件上传的正确
  50.                 if ($this->files['error'] != 0)
  51.                 {
  52.                         // echo '上传错误';
  53.                         throw new \Exception('上传错误,错误号码:' . $this->files['error']);
  54.                 }
  55.                
  56.                 // 返回对象本身
  57.                 return $this;
  58.         }

  59.         // 判断大小
  60.         protected function checkSize()
  61.         {
  62.                 // 2. 限定大小
  63.                 if ($this->config['allowSize'] > 0 && $this->files['size'] > $this->config['allowSize']) {
  64.                         // echo '超出大小';
  65.                         throw new \Exception('超出大小,限定大小为:' . $this->config['allowSize']);
  66.                 }

  67.                 // 返回对象本身
  68.                 return $this;
  69.         }

  70.         // 判断类型
  71.         protected function checkType()
  72.         {
  73.                 $allowType = $this->config['allowType'];

  74.                 // 3. 限定类型
  75.                 if (count($allowType) > 0 && !in_array($this->files['type'], $allowType)) {
  76.                         // echo '类型不允许';
  77.                         throw new \Exception('类型不允许,请检查配置文件!');
  78.                 }

  79.                 // 返回对象本身
  80.                 return $this;
  81.         }

  82.         // 判断合法性
  83.         protected function isUploadFile()
  84.         {
  85.                 // 4. 判断是否合法上传
  86.                 if (!is_uploaded_file($this->files['tmp_name'])) {
  87.                         throw new \Exception('非法上传!');
  88.                 }

  89.                 // 返回对象本身
  90.                 return $this;
  91.         }

  92.         // 生成随机名字
  93.         protected function makeFileName()
  94.         {
  95.                 // 生成随机的文件名字
  96.                 $this->fileName = md5(mt_rand(0, 1000000) . '_' . time() . '_' . uniqid());

  97.                 // 返回对象本身
  98.                 return $this;
  99.         }

  100.         // 获取拓展名
  101.         protected function extension()
  102.         {
  103.                 // 获取拓展名
  104.                 $this->ext = pathinfo($this->files['name'], PATHINFO_EXTENSION);

  105.                 // 返回对象本身
  106.                 return $this;
  107.         }

  108.         // 执行上传
  109.         public function uploadFile()
  110.         {
  111.                 // 链式调用
  112.                 $this->checkError()->checkSize()->checkType()->isUploadFile()->makeFileName()->extension();

  113.                 $saveTo = $this->config['uploadDir'] . $this->fileName . '.' . $this->ext;

  114.                 // 5. 执行上传
  115.                 if (!move_uploaded_file($this->files['tmp_name'], $saveTo)) {
  116.                         throw new \Exception('执行失败,未知错误!');
  117.                 }

  118.                 return true;
  119.         }

  120. }

  121. class uploadNew extends Uploads
  122. {
  123.         // 执行上传
  124.         public function uploadFile()
  125.         {
  126.                 // 链式调用
  127.                 $this->checkError()->isUploadFile()->makeFileName()->extension();

  128.                 $saveTo = $this->config['uploadDir'] . $this->fileName . '.' . $this->ext;

  129.                 // 5. 执行上传
  130.                 if (!move_uploaded_file($this->files['tmp_name'], $saveTo)) {
  131.                         throw new \Exception('执行失败,未知错误!');
  132.                 }

  133.                 return true;
  134.         }
  135. }

  136. try {

  137.         $config = [
  138.                 // 'allowSize' => 20,
  139.         ];

  140.         $upload = new uploadNew('image', $config);

  141.         $result = $upload->uploadFile();

  142.         var_dump($result);
  143. } catch(Exception $e) {

  144.         // 日志记录的操作

  145.         echo $e->getMessage() . '<br>';
  146.         // echo $e->getCode() . '<br>';
  147.         // echo $e->getFile() . '<br>';
  148.         // echo $e->getLine() . '<br>';
  149. }
复制代码
2、容易拓展
  1. <?php

  2. // 文件上传类
  3. class Uploads
  4. {
  5.         // 文件上传的信息
  6.         protected $files = [];

  7.         // 上传配置
  8.         protected $config = [
  9.                 // 默认限定大小,为0表示不限制
  10.                 'allowSize' => 0,
  11.                 // 默认限定类型,为空表示不限制
  12.                 'allowType' => [],
  13.                 // 上传文件保存的目录
  14.                 'uploadDir' => './upload/',
  15.         ];

  16.         // 随机名字的存储
  17.         protected $fileName = '';

  18.         // 文件的拓展名
  19.         protected $ext = '';

  20.         // 构造方法
  21.         public function __construct(string $field, array $config = [])
  22.         {
  23.                 // 保证存在字段
  24.                 if (!isset($_FILES[$field])) {
  25.                         throw new \Exception('上传文件的信息不存在!');
  26.                 }

  27.                 // 获取上传文件的信息
  28.                 $this->files = $_FILES[$field];

  29.                 // 合并配置
  30.                 $this->config = array_merge($this->config, $config);

  31.                 // 创建保存目录
  32.                 $this->makedir();
  33.         }

  34.         // 创建保存目录
  35.         protected function makeDir()
  36.         {
  37.                 // 处理保存文件的路径
  38.                 $this->config['uploadDir'] = rtrim($this->config['uploadDir'], '/') . '/';

  39.                 // 确保这个路径存在
  40.                 if (!file_exists($this->config['uploadDir'])) {
  41.                         if (!mkdir($this->config['uploadDir'], 0777, true)) {
  42.                                 throw new \Exception('创建保存目录失败,请检查权限问题!');
  43.                         }
  44.                 }
  45.         }

  46.         // 错误判断
  47.         protected function checkError()
  48.         {
  49.                 // 1. 确保文件上传的正确
  50.                 if ($this->files['error'] != 0)
  51.                 {
  52.                         // echo '上传错误';
  53.                         throw new \Exception('上传错误,错误号码:' . $this->files['error']);
  54.                 }
  55.                
  56.                 // 返回对象本身
  57.                 return $this;
  58.         }

  59.         // 判断大小
  60.         protected function checkSize()
  61.         {
  62.                 // 2. 限定大小
  63.                 if ($this->config['allowSize'] > 0 && $this->files['size'] > $this->config['allowSize']) {
  64.                         // echo '超出大小';
  65.                         throw new \Exception('超出大小,限定大小为:' . $this->config['allowSize']);
  66.                 }

  67.                 // 返回对象本身
  68.                 return $this;
  69.         }

  70.         // 判断类型
  71.         protected function checkType()
  72.         {
  73.                 $allowType = $this->config['allowType'];

  74.                 // 3. 限定类型
  75.                 if (count($allowType) > 0 && !in_array($this->files['type'], $allowType)) {
  76.                         // echo '类型不允许';
  77.                         throw new \Exception('类型不允许,请检查配置文件!');
  78.                 }

  79.                 // 返回对象本身
  80.                 return $this;
  81.         }

  82.         // 判断合法性
  83.         protected function isUploadFile()
  84.         {
  85.                 // 4. 判断是否合法上传
  86.                 if (!is_uploaded_file($this->files['tmp_name'])) {
  87.                         throw new \Exception('非法上传!');
  88.                 }

  89.                 // 返回对象本身
  90.                 return $this;
  91.         }

  92.         // 生成随机名字
  93.         protected function makeFileName()
  94.         {
  95.                 // 生成随机的文件名字
  96.                 $this->fileName = md5(mt_rand(0, 1000000) . '_' . time() . '_' . uniqid());

  97.                 // 返回对象本身
  98.                 return $this;
  99.         }
  100.        
  101.         protected function makeFileNameV2()
  102.         {
  103.                 // 生成随机的文件名字
  104.                 $this->fileName = md5(mt_rand(0, 1000000) . '_' . time() . '_' . uniqid()) . md5(time());

  105.                 // 返回对象本身
  106.                 return $this;
  107.         }


  108.         // 获取拓展名
  109.         protected function extension()
  110.         {
  111.                 // 获取拓展名
  112.                 $this->ext = pathinfo($this->files['name'], PATHINFO_EXTENSION);

  113.                 // 返回对象本身
  114.                 return $this;
  115.         }

  116.         // 执行上传
  117.         public function uploadFile()
  118.         {
  119.                 // 链式调用
  120.                 $this->checkError()->checkSize()->checkType()->isUploadFile()->makeFileName()->extension();

  121.                 $saveTo = $this->config['uploadDir'] . $this->fileName . '.' . $this->ext;

  122.                 // 5. 执行上传
  123.                 if (!move_uploaded_file($this->files['tmp_name'], $saveTo)) {
  124.                         throw new \Exception('执行失败,未知错误!');
  125.                 }

  126.                 return true;
  127.         }

  128.         public function uploadFilev2()
  129.         {
  130.         // 链式调用
  131.                 $this->checkError()->checkSize()->checkType()->isUploadFile()->makeFileNamev2()->extension();

  132.                 $saveTo = $this->config['uploadDir'] . $this->fileName . '.' . $this->ext;

  133.                 // 5. 执行上传
  134.                 if (!move_uploaded_file($this->files['tmp_name'], $saveTo)) {
  135.                         throw new \Exception('执行失败,未知错误!');
  136.                 }

  137.                 return true;
  138.         }

  139.         public function getFileName()
  140.         {
  141.                 return $this->fileName;
  142.         }

  143. }



  144. try {

  145.         $config = [
  146.                 // 'allowSize' => 20,
  147.         ];

  148.         $upload = new Uploads('image', $config);

  149.         $result = $upload->uploadFilev2();

  150.         echo $upload->getFileName();


  151.         var_dump($result);
  152. } catch(Exception $e) {

  153.         // 日志记录的操作

  154.         echo $e->getMessage() . '<br>';
  155.         // echo $e->getCode() . '<br>';
  156.         // echo $e->getFile() . '<br>';
  157.         // echo $e->getLine() . '<br>';
  158. }
复制代码
3.继承基类
  1. <?php

  2. class UploadsException extends \Exception
  3. {

  4. }

  5. // 文件上传类
  6. class Uploads
  7. {
  8.         // 文件上传的信息
  9.         protected $files = [];

  10.         // 上传配置
  11.         protected $config = [
  12.                 // 默认限定大小,为0表示不限制
  13.                 'allowSize' => 0,
  14.                 // 默认限定类型,为空表示不限制
  15.                 'allowType' => [],
  16.                 // 上传文件保存的目录
  17.                 'uploadDir' => './upload/',
  18.         ];

  19.         // 随机名字的存储
  20.         protected $fileName = '';

  21.         // 文件的拓展名
  22.         protected $ext = '';

  23.         // 构造方法
  24.         public function __construct(string $field, array $config = [])
  25.         {
  26.                 // 保证存在字段
  27.                 if (!isset($_FILES[$field])) {
  28.                         throw new \UploadsException('上传文件的信息不存在!');
  29.                 }

  30.                 // 获取上传文件的信息
  31.                 $this->files = $_FILES[$field];

  32.                 // 合并配置
  33.                 $this->config = array_merge($this->config, $config);

  34.                 // 创建保存目录
  35.                 $this->makedir();
  36.         }

  37.         // 创建保存目录
  38.         protected function makeDir()
  39.         {
  40.                 // 处理保存文件的路径
  41.                 $this->config['uploadDir'] = rtrim($this->config['uploadDir'], '/') . '/';

  42.                 // 确保这个路径存在
  43.                 if (!file_exists($this->config['uploadDir'])) {
  44.                         if (!mkdir($this->config['uploadDir'], 0777, true)) {
  45.                                 throw new \UploadsException('创建保存目录失败,请检查权限问题!');
  46.                         }
  47.                 }
  48.         }

  49.         // 错误判断
  50.         protected function checkError()
  51.         {
  52.                 // 1. 确保文件上传的正确
  53.                 if ($this->files['error'] != 0)
  54.                 {
  55.                         // echo '上传错误';
  56.                         throw new \UploadsException('上传错误,错误号码:' . $this->files['error']);
  57.                 }
  58.                
  59.                 // 返回对象本身
  60.                 return $this;
  61.         }

  62.         // 判断大小
  63.         protected function checkSize()
  64.         {
  65.                 // 2. 限定大小
  66.                 if ($this->config['allowSize'] > 0 && $this->files['size'] > $this->config['allowSize']) {
  67.                         // echo '超出大小';
  68.                         throw new \UploadsException('超出大小,限定大小为:' . $this->config['allowSize']);
  69.                 }

  70.                 // 返回对象本身
  71.                 return $this;
  72.         }

  73.         // 判断类型
  74.         protected function checkType()
  75.         {
  76.                 $allowType = $this->config['allowType'];

  77.                 // 3. 限定类型
  78.                 if (count($allowType) > 0 && !in_array($this->files['type'], $allowType)) {
  79.                         // echo '类型不允许';
  80.                         throw new \UploadsException('类型不允许,请检查配置文件!');
  81.                 }

  82.                 // 返回对象本身
  83.                 return $this;
  84.         }

  85.         // 判断合法性
  86.         protected function isUploadFile()
  87.         {
  88.                 // 4. 判断是否合法上传
  89.                 if (!is_uploaded_file($this->files['tmp_name'])) {
  90.                         throw new \UploadsException('非法上传!');
  91.                 }

  92.                 // 返回对象本身
  93.                 return $this;
  94.         }

  95.         // 生成随机名字
  96.         protected function makeFileName()
  97.         {
  98.                 // 生成随机的文件名字
  99.                 $this->fileName = md5(mt_rand(0, 1000000) . '_' . time() . '_' . uniqid());

  100.                 // 返回对象本身
  101.                 return $this;
  102.         }

  103.         // 获取拓展名
  104.         protected function extension()
  105.         {
  106.                 // 获取拓展名
  107.                 $this->ext = pathinfo($this->files['name'], PATHINFO_EXTENSION);

  108.                 // 返回对象本身
  109.                 return $this;
  110.         }

  111.         // 执行上传
  112.         public function uploadFile()
  113.         {
  114.                 // 链式调用
  115.                 $this->checkError()->checkSize()->checkType()->isUploadFile()->makeFileName()->extension();

  116.                 $saveTo = $this->config['uploadDir'] . $this->fileName . '.' . $this->ext;

  117.                 // 5. 执行上传
  118.                 if (!move_uploaded_file($this->files['tmp_name'], $saveTo)) {
  119.                         throw new \UploadsException('执行失败,未知错误!');
  120.                 }

  121.                 return true;
  122.         }

  123.         // 获取名字
  124.         public function getFileName()
  125.         {
  126.                 return $this->fileName;
  127.         }

  128. }

  129. try {

  130.         $config = [
  131.                 'allowSize' => 20,
  132.         ];

  133.         $upload = new Uploads('image', $config);

  134.         $result = $upload->uploadFile();

  135.         var_dump($result);

  136. } catch(UploadsException $e) {
  137.         echo '<pre>';
  138.                 print_r($e);
  139.         echo '</pre>';

  140.         // 日志记录的操作

  141.         echo $e->getMessage() . '<br>';
  142.         // echo $e->getCode() . '<br>';
  143.         // echo $e->getFile() . '<br>';
  144.         // echo $e->getLine() . '<br>';
  145. }
复制代码
4.多层catch
  1. <?php

  2. class UploadsException extends \Exception
  3. {

  4. }

  5. // 文件上传类
  6. class Uploads
  7. {
  8.         // 文件上传的信息
  9.         protected $files = [];

  10.         // 上传配置
  11.         protected $config = [
  12.                 // 默认限定大小,为0表示不限制
  13.                 'allowSize' => 0,
  14.                 // 默认限定类型,为空表示不限制
  15.                 'allowType' => [],
  16.                 // 上传文件保存的目录
  17.                 'uploadDir' => './upload/',
  18.         ];

  19.         // 随机名字的存储
  20.         protected $fileName = '';

  21.         // 文件的拓展名
  22.         protected $ext = '';

  23.         // 构造方法
  24.         public function __construct(string $field, array $config = [])
  25.         {
  26.                 // 保证存在字段
  27.                 if (!isset($_FILES[$field])) {
  28.                         throw new \UploadsException('上传文件的信息不存在!');
  29.                 }

  30.                 // 获取上传文件的信息
  31.                 $this->files = $_FILES[$field];

  32.                 // 合并配置
  33.                 $this->config = array_merge($this->config, $config);

  34.                 // 创建保存目录
  35.                 $this->makedir();
  36.         }

  37.         // 创建保存目录
  38.         protected function makeDir()
  39.         {
  40.                 // 处理保存文件的路径
  41.                 $this->config['uploadDir'] = rtrim($this->config['uploadDir'], '/') . '/';

  42.                 // 确保这个路径存在
  43.                 if (!file_exists($this->config['uploadDir'])) {
  44.                         if (!mkdir($this->config['uploadDir'], 0777, true)) {
  45.                                 throw new \UploadsException('创建保存目录失败,请检查权限问题!');
  46.                         }
  47.                 }
  48.         }

  49.         // 错误判断
  50.         protected function checkError()
  51.         {
  52.                 // 1. 确保文件上传的正确
  53.                 if ($this->files['error'] != 0)
  54.                 {
  55.                         // echo '上传错误';
  56.                         throw new \UploadsException('上传错误,错误号码:' . $this->files['error']);
  57.                 }
  58.                
  59.                 // 返回对象本身
  60.                 return $this;
  61.         }

  62.         // 判断大小
  63.         protected function checkSize()
  64.         {
  65.                 // 2. 限定大小
  66.                 if ($this->config['allowSize'] > 0 && $this->files['size'] > $this->config['allowSize']) {
  67.                         // echo '超出大小';
  68.                         throw new \UploadsException('超出大小,限定大小为:' . $this->config['allowSize']);
  69.                 }

  70.                 // 返回对象本身
  71.                 return $this;
  72.         }

  73.         // 判断类型
  74.         protected function checkType()
  75.         {
  76.                 $allowType = $this->config['allowType'];

  77.                 // 3. 限定类型
  78.                 if (count($allowType) > 0 && !in_array($this->files['type'], $allowType)) {
  79.                         // echo '类型不允许';
  80.                         throw new \UploadsException('类型不允许,请检查配置文件!');
  81.                 }

  82.                 // 返回对象本身
  83.                 return $this;
  84.         }

  85.         // 判断合法性
  86.         protected function isUploadFile()
  87.         {
  88.                 // 4. 判断是否合法上传
  89.                 if (!is_uploaded_file($this->files['tmp_name'])) {
  90.                         throw new \UploadsException('非法上传!');
  91.                 }

  92.                 // 返回对象本身
  93.                 return $this;
  94.         }

  95.         // 生成随机名字
  96.         protected function makeFileName()
  97.         {
  98.                 // 生成随机的文件名字
  99.                 $this->fileName = md5(mt_rand(0, 1000000) . '_' . time() . '_' . uniqid());

  100.                 // 返回对象本身
  101.                 return $this;
  102.         }

  103.         // 获取拓展名
  104.         protected function extension()
  105.         {
  106.                 // 获取拓展名
  107.                 $this->ext = pathinfo($this->files['name'], PATHINFO_EXTENSION);

  108.                 // 返回对象本身
  109.                 return $this;
  110.         }

  111.         // 执行上传
  112.         public function uploadFile()
  113.         {
  114.                 // 链式调用
  115.                 $this->checkError()->checkSize()->checkType()->isUploadFile()->makeFileName()->extension();

  116.                 $saveTo = $this->config['uploadDir'] . $this->fileName . '.' . $this->ext;

  117.                 // 5. 执行上传
  118.                 if (!move_uploaded_file($this->files['tmp_name'], $saveTo)) {
  119.                         throw new \UploadsException('执行失败,未知错误!');
  120.                 }

  121.                 return true;
  122.         }

  123.         // 获取名字
  124.         public function getFileName()
  125.         {
  126.                 return $this->fileName;
  127.         }

  128. }

  129. try {

  130.         $config = [
  131.                 // 'allowSize' => 20,
  132.         ];

  133.         $upload = new Uploads('image', $config);

  134.         $result = $upload->uploadFile();


  135.         if (2 > 1) {
  136.                 throw new Exception('你上传成功了?');
  137.         }
  138.        
  139.         var_dump($result);

  140. } catch(UploadsException $e) {

  141.         // 日志记录的操作
  142.         echo $e->getMessage() . '<br>';
  143.         // echo $e->getCode() . '<br>';
  144.         // echo $e->getFile() . '<br>';
  145.         // echo $e->getLine() . '<br>';
  146. } catch(Exception $e) {
  147.         echo $e->getMessage() . '<br>';
  148. }

  149. echo '脚本结束!';
复制代码
  1. <?php

  2. /*
  3.         异常处理
  4.                 当代码发送错误之后,我们可以针对错误改变原来的执行流程,同时异常处理可以让我们跟踪错误节点。

  5.         try

  6.         catch

  7.         Exception
  8.                 它是系统所有异常的基类
  9. */
复制代码
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.         <meta charset="UTF-8">
  5.         <title>Document</title>
  6. </head>
  7. <body>

  8.         <form action="./uploads.php" method="post" enctype="multipart/form-data">
  9.                 <input type="file" name="image">
  10.                 <input type="submit">
  11.         </form>
  12. </body>
  13. </html>
复制代码
  1. <?php

  2. class UploadsException extends \Exception
  3. {

  4. }

  5. // 文件上传类
  6. class Uploads
  7. {
  8.         // 文件上传的信息
  9.         protected $files = [];

  10.         // 上传配置
  11.         protected $config = [
  12.                 // 默认限定大小,为0表示不限制
  13.                 'allowSize' => 0,
  14.                 // 默认限定类型,为空表示不限制
  15.                 'allowType' => [],
  16.                 // 上传文件保存的目录
  17.                 'uploadDir' => './upload/',
  18.         ];

  19.         // 随机名字的存储
  20.         protected $fileName = '';

  21.         // 文件的拓展名
  22.         protected $ext = '';

  23.         // 构造方法
  24.         public function __construct(string $field, array $config = [])
  25.         {
  26.                 // 保证存在字段
  27.                 if (!isset($_FILES[$field])) {
  28.                         throw new \UploadsException('上传文件的信息不存在!');
  29.                 }

  30.                 // 获取上传文件的信息
  31.                 $this->files = $_FILES[$field];

  32.                 // 合并配置
  33.                 $this->config = array_merge($this->config, $config);

  34.                 // 创建保存目录
  35.                 $this->makedir();
  36.         }

  37.         // 创建保存目录
  38.         protected function makeDir()
  39.         {
  40.                 // 处理保存文件的路径
  41.                 $this->config['uploadDir'] = rtrim($this->config['uploadDir'], '/') . '/';

  42.                 // 确保这个路径存在
  43.                 if (!file_exists($this->config['uploadDir'])) {
  44.                         if (!mkdir($this->config['uploadDir'], 0777, true)) {
  45.                                 throw new \UploadsException('创建保存目录失败,请检查权限问题!');
  46.                         }
  47.                 }
  48.         }

  49.         // 错误判断
  50.         protected function checkError()
  51.         {
  52.                 // 1. 确保文件上传的正确
  53.                 if ($this->files['error'] != 0)
  54.                 {
  55.                         // echo '上传错误';
  56.                         throw new \UploadsException('上传错误,错误号码:' . $this->files['error']);
  57.                 }
  58.                
  59.                 // 返回对象本身
  60.                 return $this;
  61.         }

  62.         // 判断大小
  63.         protected function checkSize()
  64.         {
  65.                 // 2. 限定大小
  66.                 if ($this->config['allowSize'] > 0 && $this->files['size'] > $this->config['allowSize']) {
  67.                         // echo '超出大小';
  68.                         throw new \UploadsException('超出大小,限定大小为:' . $this->config['allowSize']);
  69.                 }

  70.                 // 返回对象本身
  71.                 return $this;
  72.         }

  73.         // 判断类型
  74.         protected function checkType()
  75.         {
  76.                 $allowType = $this->config['allowType'];

  77.                 // 3. 限定类型
  78.                 if (count($allowType) > 0 && !in_array($this->files['type'], $allowType)) {
  79.                         // echo '类型不允许';
  80.                         throw new \UploadsException('类型不允许,请检查配置文件!');
  81.                 }

  82.                 // 返回对象本身
  83.                 return $this;
  84.         }

  85.         // 判断合法性
  86.         protected function isUploadFile()
  87.         {
  88.                 // 4. 判断是否合法上传
  89.                 if (!is_uploaded_file($this->files['tmp_name'])) {
  90.                         throw new \UploadsException('非法上传!');
  91.                 }

  92.                 // 返回对象本身
  93.                 return $this;
  94.         }

  95.         // 生成随机名字
  96.         protected function makeFileName()
  97.         {
  98.                 // 生成随机的文件名字
  99.                 $this->fileName = md5(mt_rand(0, 1000000) . '_' . time() . '_' . uniqid());

  100.                 // 返回对象本身
  101.                 return $this;
  102.         }

  103.         // 获取拓展名
  104.         protected function extension()
  105.         {
  106.                 // 获取拓展名
  107.                 $this->ext = pathinfo($this->files['name'], PATHINFO_EXTENSION);

  108.                 // 返回对象本身
  109.                 return $this;
  110.         }

  111.         // 执行上传
  112.         public function uploadFile()
  113.         {
  114.                 // 链式调用
  115.                 $this->checkError()->checkSize()->checkType()->isUploadFile()->makeFileName()->extension();

  116.                 $saveTo = $this->config['uploadDir'] . $this->fileName . '.' . $this->ext;

  117.                 // 5. 执行上传
  118.                 if (!move_uploaded_file($this->files['tmp_name'], $saveTo)) {
  119.                         throw new \UploadsException('执行失败,未知错误!');
  120.                 }

  121.                 return true;
  122.         }

  123.         // 获取名字
  124.         public function getFileName()
  125.         {
  126.                 return $this->fileName;
  127.         }

  128. }

  129. try {

  130.         $config = [
  131.                 // 'allowSize' => 20,
  132.         ];

  133.         $upload = new Uploads('image', $config);

  134.         $result = $upload->uploadFile();


  135.         if (2 > 1) {
  136.                 throw new Exception('你上传成功了?');
  137.         }
  138.        
  139.         var_dump($result);

  140. } catch(UploadsException $e) {

  141.         // 日志记录的操作
  142.         echo $e->getMessage() . '<br>';
  143.         // echo $e->getCode() . '<br>';
  144.         // echo $e->getFile() . '<br>';
  145.         // echo $e->getLine() . '<br>';
  146. } catch(Exception $e) {
  147.         echo $e->getMessage() . '<br>';
  148. }

  149. echo '脚本结束!';
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|外汇论坛 ( 粤ICP备16021788号 )

GMT+8, 2024-5-10 15:59 , Processed in 0.070944 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表