目录

前言

今天在公司无事,就搞了下文件上传的东东,方便自己的学习,和今后的使用,目前只支持单文件上传。下面是代码部分,也可以访问我的 gitee 或 github,不想看代码的童鞋可以直接[下载]。。。(算了,讨厌百度云,如果有需要的话留言)代码使用,使用方法请点击

代码

<?php

namespace e;

class fileupload
{
    /**
     * @var $name 上传文件标记名
     */
    protected $name = 'file';

    /**
     * @var $ext 所允许的扩展名
     */
    protected $exts  = [];

    /**
     * @var $file 上传的文件资源
     */
    protected $file = null;

    /**
     * @var $upload_success 是否上传成功标志
     */
    protected $upload_success = false;

    /**
     * @var $max_size 上传文件所允许的大小,单位为 m
     */
    protected $max_size = 2;

    /**
     * @var $error_code 错误码
     */
    protected $error_code = 0;

    /**
     * @var $error_msg 错误信息
     */
    protected $error_msg = '';

    /**
     * @var $file_ext 文件扩展名
     */
    protected $file_ext = '';

    /**
     * @var $type 文件类型,默认为任意类型
     */
    protected $type = 'file';

    protected const err_ok         = 0;
    protected const err_file_size  = 1;
    protected const err_form_size  = 2;
    protected const err_partial    = 3;
    protected const err_no_file    = 4;
    protected const err_file_exist = 5;
    protected const err_no_tmp_dir = 6;
    protected const err_cant_write = 7;
    protected const err_file_type  = 8;

    /**
     * 配置上传信息
     *
     * @param array $arr
     * @return e\fileupload
     */
    public function config($arr)
    {
        foreach ($arr as $key => $val) {
            if (property_exists($this, $key)) {
                $this->$key = $val;
            }
        }
        return $this;
    }

    /**
     * 进行文件上传操作
     *
     * @return e\fileupload
     */
    public function upload()
    {
        $this->file     = $this->getfile();
        $this->file_ext = strrchr($this->file['name'], '.');

        $this->upload_success = !($this->uploaderror() || $this->overmaxsize() || $this->notallowtype());

        return $this;
    }

    /**
     * 判断文件是否上传成功
     * 
     * @return boolean
     */
    public function uploadsuccess()
    {
        return $this->upload_success;
    }

    /**
     * 保存已上传的文件
     *
     * @param string $path
     * @param string $file_name
     *
     * @return boolean
     */
    public function save($path, $file_name = '')
    {
        if (!$this->uploadsuccess()) {
            return false;
        }

        // 判断文件夹是否存在,如果不存在,新建一个文件夹
        if (!is_dir($path)) {
            mkdir($path);
        }

        // 获取文件名,不包含后缀
        $file_name = $file_name ?: 'e_' . time() . mt_rand(10000, 99999);

        $file = rtrim($path, '/') . '/' . $file_name . $this->file_ext;

        // 判断文件是否存在
        if (file_exists($file)) {
            $this->error_code = self::err_file_exist;
            return false;
        }

        if (move_uploaded_file($this->file['tmp_name'], $file)) {
            return true;
        }

        // 文件未上传成功,出现未知错误
        $this->error_code = -1;
        return false;
    }

    /**
     * 返回错误码
     * 
     * @return integer
     */
    public function errorcode()
    {
        return $this->error_code;
    }

    /**
     * 返回错误信息
     *
     * @return string
     */
    public function errormsg()
    {
        !$this->error_msg && $this->seterrormsgbycode($this->error_code);

        return $this->error_msg;
    }

    /**
     * 获取上传文件
     * 
     * @return mixed
     */
    protected function getfile()
    {
        return $_files[$this->name];
    }

    /**
     * 判断是否上传错误
     * 
     * @return boolean
     */
    protected function uploaderror()
    {
        $this->error_code = $this->file['error'];

        return (bool)$this->file['error'];
    }

    /**
     * 根据错误代码设置错误信息
     *
     * @param  int $code
     */
    protected function seterrormsgbycode($code)
    {
        $msg_arr = [
            '',
            '上传文件最大为 ' . $this->getmaxsize() . 'm',
            '上传文件过大',
            '文件只有部分被上传',
            '没有文件被上传',
            '文件已存在',
            '文件丢失',
            '文件写入失败',
            '只能上传' . implode(',', $this->exts) . '类型的文件'
        ];

        $this->error_msg = $msg_arr[$code] ?? '未知错误';
    }

    /**
     * 获取上传文件所限制的最大大小
     * 
     * @return int
     */
    protected function getmaxsize()
    {
        return min($this->max_size, (int)ini_get('upload_max_filesize'));
    }

    /**
     * 判断文件是否超出限制大小
     *
     * @return boolean
     */
    protected function overmaxsize()
    {
        if ($this->file['size'] > $this->getmaxsize() * 1024 * 1024 * 8) {
            $this->error_code = self::err_file_size;
            return true;
        }
        return false;
    }

    /**
     * 通过类型获取后缀名数组
     *
     * @return array
     */
    protected function getextsbytype()
    {
        $exts_arr = [
            'image' => ['jpg', 'jpeg', 'png', 'gif'],
            'file'  => [],
            'zip'   => ['zip', 'rar', '7z']
        ];

        return $exts_arr[$this->type] ?? [];
    }

    /**
     * 判断是否是允许的类型
     * 
     * @return boolean
     */
    protected function notallowtype()
    {
        $this->exts = $this->exts ?: $this->getextsbytype();

        if (!$this->exts) return false;

        if (preg_match('/^\.' . implode('|', $this->exts) . '$/i', $this->file_ext)) {
            return false;
        }

        $this->error_code = self::err_file_type;
        return true;
    }
}

使用方法

  1. 引入文件
include 'fileupload.class.php';
  1. 实例化类
$uploader = new e\fileupload();
  1. 配置
$uploader->config([
    'name'      => 'file',
    'exts'      => ['jpg'],
    'type'      => 'image',
    'max_size'  => 2,
]);

配置项的说明:

配置项 说明
name 是 html 中 input 的 name,例如: <input type=”file” name=”file”>,默认是 file
exts 所允许上传的文件扩展名,类型为数组,默认是任何类型的文件
type 上传文件类型,可以通过设置 type 属性,设置允许上传类型,默认为 file,目前可选类型有:file 全部文件类型,image 图片类型, zip 压缩包,注意:exts优先级高于 type,即如果设置了 type 为 file,exts 为 [‘jpg’] 时,也还是只能上传 jpg 类型的文件的
max_size 所允许上传的大小,单位为 m,默认为 2m
  1. 上传
$uploader->upload();
  1. 保存
$uploader->save('./uploads/', 'test')

save 方法的返回值是一个布尔值,上传成功返回 true,失败返回 false,可以根据返回的状态进行相应的操作,如果失败,可以使用 errorcode() 方法获取错误代码,使用 errormsg() 方法获取错误信息。

save 方法的第一个参数为必填参数,是要保存的路径,第二个参数是文件名,可选,如果不填,则会自动生成,生成规则: e_ 连接上当前时间的时间戳再连接5位随机数

也可以使用链式操作:

$uploader->upload()->save('./uploads/', 'test');

错误码说明

错误码 说明
0 无错误
1 上传文件超出限制大小
2 上传文件超出 form 表单所设置的 max_file_size,也是文件过大
3 文件只有部分被上传
4 没有文件被上传
5 存在同名文件
6 文件丢失
7 文件写入失败
8 上传的文件类型不被允许
-1 未知错误