PHP教程之php文件上传类
PHP教程之php文件上传类
czk19900520 发表于4个月前
PHP教程之php文件上传类
  • 发表于 4个月前
  • 阅读 1
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云实验室 1小时搭建人工智能应用,让技术更容易入门 免费体验 >>>   

/*

*千锋PHP独家

*/

class Upload { //保存路径 protected $savePath = './'; //日期目录 protected $datePath = true; //随机名字 protected $randName = true; //默认后缀 protected $extension = 'png'; //允许MIME protected $mimes = ['image/png','image/jpeg','image/gif']; //允许后缀 protected $suffixes = ['png','jpg','jpeg','gif']; //最大尺寸 protected $maxSize = 2000000; //错误代码 protected $errorNumber = 0; //错误信息 protected $errorMessage = '上传成功'; //上传信息 protected $uploadInfo; //文件名称 protected $pathName; public function __construct($options=null) { $this->setOption($options); }

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。互联网+时代,时刻要保持学习,携手千锋PHP,Dream It Possible。

public function setOption($options) { if (is_array($options)) { $keys = get_class_vars(__CLASS__); foreach ($options as $key => $value) { //在属性列表中才设置属性 if (in_array($key, $keys)) { $this->$key = $value; } } } } public function uploadFile($field) { //1、检查保存路径 if (!$this->checkSavePath()) { return false; } //2、检查上传信息 if (!$this->checkUploadInfo($field)) { return false; } //3、检查系统错误 if (!$this->checkUploadError()) { return false; } //4、检查自定义错误 if (!$this->checkAllowOption()) { return false; } //5、检车是否是上传文件 if (!$this->checkUploadFile()) { return false; } //6、拼接路径名 $this->joinPathName(); //7、移动上传文件 if (!$this->moveUploadFile()) { return false; } return true; } protected function checkSavePath() { if (!is_dir($this->savePath)) { $this->errorNumber = -1; $this->errorMessage = '保存路径不存在'; return false; } if (!is_writable($this->savePath)) { $this->errorNumber = -2; $this->errorMessage = '保存路径不可写'; return false; } $this->savePath = rtrim($this->savePath,'/') . '/'; return true; } protected function checkUploadInfo($field) { if (empty($_FILES[$field])) { $this->errorNumber = -3; $this->errorMessage = '没有'.$field.'相关上传信息'; return false; } $this->uploadInfo = $_FILES[$field]; return true; } protected function checkUploadError() { if ($this->uploadInfo['error'] == 0) { return true; } switch ($this->uploadInfo['error']) { case UPLOAD_ERR_INI_SIZE: $this->errorMessage = '超出了配置文件中设定文件大小'; break; case UPLOAD_ERR_FORM_SIZE: $this->errorMessage = '超出了MAX_FILE_SIZE的大小'; break; case UPLOAD_ERR_PARTIAL: $this->errorMessage = '只有部分文件被上传'; break; case UPLOAD_ERR_NO_FILE: $this->errorMessage = '没有文件被上传'; break; case UPLOAD_ERR_NO_TMP_DIR: $this->errorMessage = '没有找到临时文件夹'; break; case UPLOAD_ERR_CANT_WRITE: $this->errorMessage = '文件写入失败'; break; default: $this->errorMessage = '未知错误'; break; } $this->errorNumber = $this->uploadInfo['error']; return false; } protected function checkAllowOption() { if (!in_array($this->uploadInfo['type'],$this->mimes)) { $this->errorNumber = -4; $this->errorMessage = '不允许的MIME:'.$this->uploadInfo['type']; return false; } if (!in_array($this->extension,$this->suffixes)) { $this->errorNumber = -5; $this->errorMessage = '不允许的后缀:'.$this->extension; return false; } if ($this->uploadInfo['size'] > $this->maxSize) { $this->errorNumber = -6; $this->errorMessage = '超出了规定大小:'.$this->maxSize.'字节'; return false; } return true; } protected function checkUploadFile() { if (!is_uploaded_file($this->uploadInfo['tmp_name'])) { $this->errorNumber = -7; $this->errorMessage = '不是上传文件'; return false; } return true; } protected function joinPathName() { //路径 $this->pathName = $this->savePath; if ($this->datePath) { $this->pathName .= date('Y/m/d/'); if (!file_exists($this->pathName)) { mkdir($this->pathName,0777,true); } } //名字 if ($this->randName) { $this->pathName .= uniqid(); } else { $info = pathinfo($this->uploadInfo['name']); $this->pathName .= $info['filename']; } //后缀 $this->pathName .= '.' . $this->extension; } protected function moveUploadFile() { if (move_uploaded_file($this->uploadInfo['tmp_name'], $this->pathName)) { return true; } $this->errorNumber = -8; $this->errorMessage = '上传文件保存'; return false; } }

标签: 千锋PHP
共有 人打赏支持
粉丝 0
博文 10
码字总数 0
×
czk19900520
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: