您的位置:首页 > 编程语言 > PHP开发

PHP中面向对象的文件上传类

2016-09-20 21:34 302 查看
<?php
/**
*文件上传类
*
**/

class Upload
{
//上传到哪个目录
protected $path = './upload/';
//准许的MIME
protected $allowmime = ['image/png','image/jpg','image/jpeg','image/pjpeg','image/bmp','image/wbmp','image/gif','image/x-png'];
//准许的后缀
protected $allowsubfix = ['jpg','png','jpeg','gif','bmp'];
//准许的大小
protected $allowsize = 2097152;
//是否准许随机文件名
protected $israndname = true;
//是否准许日期目录
protected $isdatepath = true;
//文件的大小
protected $size;
//文件的原名
protected $orgname;
//文件的新名
protected $newname;
//文件的后缀
protected $subfix;
//文件的MIME类型
protected $mime;
//文件的新路径
protected $newpath;
//错误号
protected $errorno;
//错误信息
protected $errorinfo;
//临时文件
protected $tmpfile;
//前缀
protected $prefix;

//初使化成员属性,传进来的东西特别多,我们要求以数组的形式来传递
//foreach循环,取出键名、值
//将键名设置为成员属性名,将键值设为成员属性值
//注意键名的合法性
// errorInfo   errorinfo
public function __construct(Array $config = [])
{
foreach ($config as $key => $value) {
$this->setOption($key,$value);
}

}

protected function setOption($key,$value)
{
$key = strtolower($key);

$allPro = get_class_vars(get_class($this));

if (array_key_exists($key,$allPro)) {
$this->$key = $value;
}
}

//设置一个get方法专门获得新路径

//设置 一个get方法专门来获得错误信息

//成员方法上传方法
public function uploadFile($field)
{

//检测路径用户是否定义过,如果没有定义失败
if (!file_exists($this->path)) {
$this->setOption('errorNo',-1);
return false;
}

//目录权限检测
if (!$this->checkPath()) {
$this->setOption('errorNo',-2);
return false;

}

//获得$_FILES当中五个基本信息
$name = $_FILES[$field]['name'];
$size = $_FILES[$field]['size'];
$type = $_FILES[$field]['type'];
$tmpName = $_FILES[$field]['tmp_name'];
$error = $_FILES[$field]['error'];

//传入到一个成员方法里面进行设置
if (!$this->setFiles($name,$size,$type,$tmpName,$error)) {
return false;
}

//检测MIME是否合法,检测后缀是否合法,检测文件大小是否超过了自定义的大小
if (!$this->checkMime() || !$this->checkSubfix() || !$this->checkSize()) {
return false;
}

//新名
$this->newname = $this->getNewName();

//新路径处理
$this->newpath = $this->getNewPath();

//是否是上传文件
//如果是上传文件移动上传文件至指定的目录
//如果成员return true
return $this->move();

}

protected function move()
{
if (!is_uploaded_file($this->tmpfile)) {
$this->setOption('errorNo',-6);
return false;
}

if (move_uploaded_file($this->tmpfile,$this->newpath.$this->newname)) {
return true;
} else {
$this->setOption('errorNo',-7);
return false;
}

}

protected function getNewPath()
{
$this->path = rtrim($this->path,'/').'/';

if ($this->isdatepath) {
$newpath = $this->path.date('Y/m/d/');
if (!file_exists($newpath)) {
mkdir($newpath,0755,true);
}
return $newpath;
} else {
return $this->path;
}

}

protected function getNewName()
{
if ($this->israndname) {
return $this->prefix . uniqid() . '.' . $this->subfix;
} else {
return $this->prefix . $this->orgname;
}
}

protected function checkSize()
{
if ($this->size > $this->allowsize) {

$this->setOption('errorNo',-5);
return false;
} else {

return true;
}

}

protected function checkSubFix()
{

if (in_array($this->subfix,$this->allowsubfix)) {

return true;
} else {

$this->setOption('errorNo',-4);
return false;
}
}

protected function checkMime()
{
if (in_array($this->mime,$this->allowmime)) {

return true;
} else {

$this->setOption('errorNo',-3);
return false;
}

}

protected function setFiles($name,$size,$type,$tmpName,$error) {
//1 2 3 4 6 7  0(正常)
if ($error) {
$this->setOption('errorNo',$error);
return false;
}

$this->orgname = $name;
$this->size = $size;
$this->tmpfile = $tmpName;
$this->mime = $type;

//后缀没处理
$info = pathinfo($name);
$this->subfix = $info['extension'];

return true;
/*
$arr = explode('.',$name);
$this->subfix = array_pop($arr);

$arr = explode('.',$name);
$this->subfix = $arr[count($arr)-1];

$pos = strrpos($name,'.');
echo substr($name,$pos + 1);
*/
}

protected function checkPath()
{
//检测路径是否是目录,如果不存在创建
if (!is_dir($this->path)) {
return mkdir($this->path,0755,true);
}
//检测路径是否可写,如果不写写更改权限
if (!is_writeable($this->path) ||  !is_readable($this->path)) {
return chmod($this->path,0755);
}
return true;
}

protected function getErrorInfo()
{
switch ($this->errorno) {
case 1:
$str = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
break;
case 2:
$str = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
break;
case 3:
$str = '部份件被上传';
break;
case 4:
$str = '没有文件被上传';
break;
case 6:
$str = '找不到临时文件夹';
break;
case 7:
$str = '临时文件写入失败';
break;
case -1:
$str = '自定义的上传路径不存在';
break;
case -2:
$str = '没有权限';
break;
case -3:
case -4:
$str = '类型或后缀不准许';
break;
case -5:
$str = '超过了自定义的大小';
break;
case -6:
$str = '不是上传文件';
break;
case -7:
$str = '移动上传文件失败';
break;

}
return $str;
}

public function __get($key)
{
if (in_array($key, ['newpath','newname','errorno','size'])) {
return $this->$key;
} else if ($key == 'errorinfo') {
return $this->getErrorInfo();
}
}
}

//调用:

$upload = new Upload();

$result = $upload->uploadFile('f');

var_dump($result);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PHP 文件 上传类