微型php框架 library/upload.class.php

发布时间:2020-06-24 14:00:55 作者:柯岳ky
来源:网络 阅读:231

上传类库

<?php


class upload {

    protected $allowExt = array('jpg','jpeg','gif','png','bmp');

    protected $allowSize = 1; // 最大上传大小,单位为M


    protected $errno = 0;

    protected $error = array(

        0=>'上传完成',

        1=>'文件超出upload_max_filesize',

        2=>'文件超出表单中 MAX_FILE_SIZE 选项指定的值',

        3=>'文件只有部分被上传',

        4=>'没有文件被上传',

        6=>'找不到临时目录',

        7=>'文件定入失败',

        8=>'文件大小超出配置文件的限制',

        9=>'不允许的文件类型',

        10=>'创建目录失败',

        11=>'未知错误,反思中'

    );


    // 获取后缀

    protected function getExt($file) {

        $ext = strtolower(strrchr($file,'.'));


        return $ext;

    }


    // 检验后缀

    protected function checkExt($ext) {

        return in_array(ltrim($ext,'.'),$this->allowExt);

    }


    // 检验大小

    protected function checkSize($size) {

        return $size <= $this->allowSize * 1000 * 1000;

    }


    // 按日期生成目录

    protected function mk_dir() {

        $dir = date('Y/m/d',time());

        $dir = ROOT . 'data/p_w_picpaths/' . $dir;


        if(!is_dir($dir)) {

            if(!mkdir($dir,0777,true)) {

                return false;

            }

        }


        return $dir;

    }



    // 生成随机文件名

    protected function randName($n = 6) {

        if($n <= 0) {

            return '';

        }


        $str = 'abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ0123456789';

        $str = substr(str_shuffle($str),0,$n);


        return $str;

    }


    public function up($name) {

        // $_FILES里面有没有$name指定的单元

        if(!isset($_FILES[$name])) {

            return false;

        }


        $f = $_FILES[$name];


        // 判断错误码,是否上传成功

        if(($this->errno = $f['error']) > 0) {

            return false;

        }


        // 判断大小

        if(!$this->checkSize($f['size'])) {

            $this->errno = 8;

            return false;

        }

        

        // 判断类型

        $ext = $this->getExt($f['name']);

        if(!$this->checkExt($ext)) {

            $this->errno = 9;

            return false;

        }


        // 上传,返回路径

        $path = $this->mk_dir();

        if(!$path) {

            $this->errno = 10;

            return false;

        }


        $filename = $this->randName(6);

        $path = $path . '/' . $filename . $ext;


        if(!move_uploaded_file($f['tmp_name'],$path)) {

            $this->errno = 11;

            return false;

        }

        

        $path = str_replace(ROOT,'',$path);

        return $path;

    }


    // 获取错误的接口

    public function getErr() {

        return $this->error[$this->errno];

    }


    // 设置允许的后缀

    public function setExt($arr) {

        $this->allowExt = $arr;

    }


    // 设置最大上传值

    public function setSize($num=2) {

        $this->allowSize = $num;

    }

}



/*

define('ROOT','D:/www/0713/');

$upload = new upload();


if($path = $upload->up('pic')) {

    echo $path,'<br />';

    echo '上传成功';

} else {

    echo $upload->getErr();

}


*/


推荐阅读:
  1. php类库到sublime完成
  2. 如何在 Laravel 中 “规范” 的开发验证码发送功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php 微型框架

上一篇:pycharm编码时集体向前缩进的方法

下一篇:如何在anaconda安装jieba

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》