php uploadfile类,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相关技术请搜索千锋PHP,做真实的自己,用良心做教育

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值