php-文件上传类

<span style="font-family: Arial, Helvetica, sans-serif;"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"></span>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>新建网页</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<script type="text/javascript">

</script>

<style type="text/css">
</style>
</head>
    <body>
        <h2>多文件上传案例</h2>
        <form action="01.php" method="post" enctype="multipart/form-data">
            用户名:<input type="text" name="username" value="张三" /><br />
            头像:<input type="file" name="avatar" value="" /><br />
            艳照:<input type="file" name="hotpic" value="" /><br />
            简历:<input type="file" name="jianli" value="" /><br />
            <input type="submit" value="提交" />
        </form>
    </body>
</html>


<?php
/****
燕十八 公益PHP讲堂

论  坛: http://www.zixue.it
微  博: http://weibo.com/Yshiba
YY频道: 88354001
****/



define('ACC',true);

require('include/init.php');
require(ROOT . 'tool/UpTool.class.php');


$uptool = new UpTool();
$uptool->setExt('doc');
$uptool->setSize(1);


if($res = $uptool->up('avatar')) {
    echo 'OK';
    echo $res;
} else {
    echo 'FAIL<br />';
    echo $uptool->getErr();
}


<?php
/****
燕十八 公益PHP讲堂

论  坛: http://www.zixue.it
微  博: http://weibo.com/Yshiba
YY频道: 88354001
****/



/*
单文件上传类
多文件上传由同学们自己扩展
*/

defined('ACC')||exit('ACC Denied');


/*
上传文件
配置允许的后缀
配置允许的大小
随机生成目录
随机生成文件名

获取文件后缀
判断文件的后缀.

良好的报错的支持

*/





class UpTool {
    protected $allowExt = 'jpg,jpeg,gif,bmp,png';
    protected $maxSize = 1; //1M,M为单位

    protected $errno = 0; // 错误代码
    protected $error = array(
        0=>'无错',
        1=>'上传文件超出系统限制',
        2=>'上传文件大小超出网页表单页面',
        3=>'文件只有部分被上传',
        4=>'没有文件被上传',
        6=>'找不到临时文件夹',
        7=>'文件写入失败',
        8=>'不允许的文件后缀',
        9=>'文件大小超出的类的允许范围',
        10=>'创建目录失败',
        11=>'移动失败'
            
    );

    
    public function up($key) {
        if(!isset($_FILES[$key])) {
            return false;
        }

        $f = $_FILES[$key];


        // 检验上传有没有成功
        if($f['error']) {
            $this->errno = $f['error'];
            return false;
        }


        // 获取后缀
        $ext = $this->getExt($f['name']);
        
        // 检查后缀
        if(!$this->isAllowExt($ext)) {
            $this->errno = 8;
            return false;
        }

        // 检查大小
        if(!$this->isAllowSize($f['size'])) {
            $this->errno = 9;
            return false;
        }

        // 通过

        //创建目录
        $dir = $this->mk_dir();

        if($dir == false) {
            $this->error = 10;
            return false;
        }
    
        // 生成随机文件名
        $newname = $this->randName() . '.' . $ext;
        $dir = $dir . '/' . $newname;


        // 移动
        if(!move_uploaded_file($f['tmp_name'],$dir)) {
            $this->errno = 11;
            return false;
        }

        return str_replace(ROOT,'',$dir);
    
    }
    
    
    public function getErr() {
        return $this->error[$this->errno];
    }


    /*
        parm string $exts 允许的后缀
    */
    public function setExt($exts) {
        $this->allowExt = $exts;
    }

    public function setSize($num) {
        $this->maxSize = $num;
    }

    /*
        parm String $file
        return String $ext 后缀
    */
    protected function getExt($file) {
        $tmp = explode('.',$file);
        return end($tmp);
    }

    /*
        parm String $ext 文件后缀
        return bool

        防止大小写的问题 JPG
    */
    protected function isAllowExt($ext) {
        return in_array(strtolower($ext),explode(',',strtolower($this->allowExt)));
    }


    // 检查文件的大小 
    protected function isAllowSize($size) {
        return $size <= $this->maxSize * 1024 * 1024;
    }


    /*
        按日期创建目录的方法
    */
    protected function mk_dir() {
        $dir = ROOT . 'data/images/' . date('Ym/d');

        if(is_dir($dir) || mkdir($dir,0777,true)) {
            return $dir;
        } else {
            return false;
        }
    }


    /*
        生成随机文件名
    */

    protected function randName($length = 6) {
        $str = 'abcdefghijkmnpqrstuvwxyz23456789';
        return substr(str_shuffle($str),0,$length);
    }


}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值