php文件上传

利用闲暇时间写一个简单的PHP上传类

HTML代码

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文件上传</title>
</head>
<body>
    <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="up"><br>
        <button type="submit">提交</button>
    </form>
</body>
</html>

php代码

<?php

/**
 * 图片上传类
 * Class Upload
 */


class Upload
{
    protected $path = './uploads/';  #保存路径
    protected $allowSuffix = [     #允许的后缀
        'jpg',
        'jpeg',
        'gif',
        'png',
        'wbmp'
    ];
    protected $allowMime = [       #允许的类型
        'image/jpeg',
        'image/gif',
        'image/wbmp',
        'image/png'
    ];
    protected $maxSize = 2000000;  #文件大小
    protected $isRandName = true;  #是否启用随机
    protected $preFix = 'xy_';     #文件前缀

    #错误代码和错误信息
    protected $errorCode = 999;
    protected $errorInfo = '文件上传成功';

    #文件信息
    protected $oldName; #文件名
    protected $suffix;  #文件后缀
    protected $size;    #文件大小
    protected $mime;    #文件类型
    protected $tempName;

    #文件的新名字
    protected $newName;

    public function __construct($arr = [])
    {
         foreach ($arr as $key=>$val){
            $this->setOption($key,$val);
         }
    }

    /**
     * @param $key
     * @param $val
     * 判断$Key是不是我的成员属性,如果是则设置
     */
    protected function setOption($key,$val)
    {
        $keys = array_keys(get_class_vars(__CLASS__));  #得到所有成员属性
        if(in_array($key,$keys)){  #是成员属性则设置
            $this->$key = $val;
        }
    }

    public function uploadFile($key)
    {
        #判断有没有设置路径
        if (empty($this->path)){
            $this->setOption('errorCode',-1);
            return false;
        }
        if (!$this->check()){
            $this->setOption('errorCode',-2);
            return false;
        }
        $error = $_FILES[$key]['error'];
        if($error){
            $this->setOption('errorCode',$error);
            return false;
        }else{
            $this->getFileInfo($key);
        }
        if (!$this->checkSize() || !$this->checkMime() || !$this->checkSuffix()){
            return false;
        }
        $this->newName = $this->createNewName();
        if (is_uploaded_file($this->tempName)){
            if(move_uploaded_file($this->tempName,$this->path.$this->newName)){
                return $this->path.$this->newName;
            }else{
                $this->setOption('errorCode',-7);
                return false;

            }
        }else {
            $this->setOption('errorCode',-6);
            return false;
        }
    }

    /**
     * 检验文件夹是否存在是否可写
     */
    protected function check()
    {
        if (!file_exists($this->path) || !is_dir($this->path)){
            return mkdir($this->path,0777,true);
        }
        if (!is_writeable($this->path)){
            return chmod($this->path,0777);  #修改权限
        }
        return true;
    }

    protected function getFileInfo($key)
    {
        $this->oldName = $_FILES[$key]['name'];
        $this->mime = $_FILES[$key]['type'];
        $this->tempName = $_FILES[$key]['tmp_name'];
        $this->size = $_FILES[$key]['size'];
        $this->suffix = pathinfo($this->oldName)['extension'];
    }


    /**
     * @return bool
     */
    protected function checkSize()
    {
        if($this->size > $this->maxSize){
            $this->setOption('errorCode',-3);
            return false;
        }
        return true;
    }

    protected function checkMime()
    {
        if(!in_array($this->mime,$this->allowMime)){
            $this->setOption('errorCode',-4);
            return false;
        }
        return true;
    }

    protected function checkSuffix()
    {
        if(!in_array($this->suffix,$this->allowSuffix)){
            $this->setOption('errorCode',-5);
            return false;
        }
        return true;
    }

    /**
     * @return string
     * 创建新的文件名
     */
    protected function createNewName()
    {
        if($this->isRandName){
            $name = $this->preFix.uniqid().'.'.$this->suffix;
        }else{
            $name = $this->preFix.$this->oldName;
        }
        return $name;
    }

    public function __get($name)
    {
        if ($name == 'errorCode'){
            return $this->errorCode;
        }else if ($name == 'errorInfo'){
            return $this->getErrorInfo();
        }
    }

    protected function getErrorInfo()
    {
        switch ($this->errorCode){
            case -1:
                $str = '文件路径没有设置';
                break;
            case -2:
                $str = '文件路径不是目录或者没有权限';
                break;
            case -3:
                $str = '文件大小超过指定范围';
                break;
            case -4:
                $str = '文件mime类型不符合';
                break;
            case -5:
                $str = '文件后缀不符合';
                break;
            case -6:
                $str = '不是上传文件';
                break;
            case -7:
                $str = '移动失败';
                break;
            case 1:
                $str = '超出php.ini设置大小';
                break;
            case 2:
                $str = '超出html表单的大小';
                break;
            case 3:
                $str = '文件部分上传';
                break;
            case 4:
                $str = '没有文件部上传';
                break;
            case 6:
                $str = '找不到临时文件';
                break;
            case 7:
                $str = '文件写入失败';
                break;
            case 999:
                $str = '文件上传成功';
                break;

        }
        return $str;
    }

}



$test = new Upload();
$test->uploadFile('up');

推荐学习地址

http://xiaoyupp.top/index/Muke/index
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值