php高级实战-上传文件类

upload.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div>
        <form action="./Upload.php" method="post" enctype="multipart/form-data">
            <input type="file" name="fm"><br />
            <input type="submit" value="上传">
        </form>
    </div>
</body>
</html>

upload.php

<?php

$up = new Upload();

$up->uploadFile('fm');

var_dump($up->errorNumber);
var_dump($up->errorInfo);

class Upload {
    protected $path = './upload/' ;
    protected $allowSuffix = ['jpg', 'jpeg', 'gif', 'wbmp', 'png'];
    protected $allowMime = ['image/jpeg', 'image/gif', 'image/wbmp', 'image/png'];

    protected $maxSize = 2000000;
    protected $isRandName = true;
    protected $prefix = 'up_';

    protected $errorNumber;
    protected $errorInfo;

    protected $oldName;
    protected $suffix;
    protected $size;
    protected $mime;
    protected $tmpName;

    protected $newName;

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

    protected function setOption ($key, $value) {
        $keys = array_keys(get_class_vars(__CLASS__));

        if (in_array($key, $keys)) {
            $this->$key = $value;
        }
    }

    public function uploadFile ($key) {
        if (empty($this->path)) {
            $this->setOption('errorNumber', -1);

            return false;
        }

        if (!$this->check()) {
            var_dump('check');
            $this->setOPtion('errorNumber', -2);

            return false;
        }

        $error = $_FILES[$key]['error'];
        if ($error) {
            $this->setOPtion('errorNumber', $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->tmpName)) {
            if (move_uploaded_file($this->tmpName, $this->path . $this->newName)) {
                return $this->path . $this->newName;
            } else {
                $this->setOPtion('errorNumber', -7);

                return false;
            }
        } else {
            $this->setOPtion('errorNumber', -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->tmpName = $_FILES[$key]['tmp_name'];
        $this->size = $_FILES[$key]['size'];
        $this->suffix = pathinfo($this->oldName)['extension'];

    }

    protected function checkSize () {
        if ($this->size > $this->maxSize) {
            $this->setOPtion('errorNumber', -3);

            return false;
        }

        return true;
    }

    protected function checkMime () {
        if (!in_array($this->mime, $this->allowMime)) {
            $this->setOPtion('errorNumber', -4);

            return false;
        }

        return true;
    }

    protected function checkSuffix () {
        if (!in_array($this->suffix, $this->allowSuffix)) {
            $this->setOPtion('errorNumber', -5);

            return false;
        }

        return true;
    }

    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 == 'errorNumber') {
            return $this->errorNumber;
        } else if ($name == 'errorInfo') {
            return $this->getErrorInfo();
        }
    }

    protected function getErrorInfo () {
        switch ($this->errorNumber) {
            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;
            default:
                $str = '上传成功,';
                break;
        }

        return $str;
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值