PHP设计的超强大的文件上传类(单文件上传)

Posted on 2016-04-24 21:24  Y-HKL  阅读(226)  评论(0编辑  收藏  举报

form.html

<!DOCTYPE html>
<html>
<head>
    <title>文件上传</title>
</head>
<body>
    <form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
    <input type="file" name="spic"><br>
    <input type="submit" name="sub" value="上传文件">
        
    </form>
</body>
</html>

upload.php

<?php
include "./FileUpload.class.php";

$up=new FileUpload(array('isRandName'=>true,'allowType'=>array('txt','doc','pig','gif','php','jpg'),'filePath'=>'./upload/','maxSize'=>2000000));


if($up->uploadFile('spic')){
    echo $up->getNewFileName();
}else{
    echo $up->getErrorMsg();
}
//var_dump($up);
?>

FileUpload.class.php

<?php
class FileUpload{
    //上传文件保存的路径
    private $filePath;
    //允许上传文件的类型
    private $allowType=array('gif','jpg','png','jpeg');
    //上传文件的最大尺寸  1M
    private $maxSize=1000000;   
    //是否是随机文件名
    private $isRandName=true;
    //原文件名称
    private $originName;
    //临时文件名称
    private $tmpFileName;
    //文件类型
    private $fileType;
    //文件的大小
    private $fileSize;
    //新文件名
    private $newFileName;
    //错误号
    private $errorNum;
    //用来提供错误报告
    private $errorMess;



    //用于对上传文件的初始化
    //1.指定上传的路径。2.允许的类型。3.限制大小。4.是否使用随机文件名
    //让用户可以不用按位置传参数,后面参数給值不用将前几个参数也提供值
    function __construct($options=array()){
        /*$this->filePath=$filePath;
        $this->allowType=$allowType;
        $this->maxSize=$maxSize;
        $isRandName=$isRandName;*/
        foreach ($options as $key => $value) {
            //将传过来的$key全转变成小写
            //$key=strtolower($key);
            //查看用户参数中数组的下标是否和成员属性名相同
            if(!in_array($key,get_class_vars(get_class($this)))){
                continue;
            }
            $this->setOption($key,$value);
        }
        /*print_r(get_class_vars(get_class($this)));*/

    }

    private function setOption($key,$value){
        $this->$key=$value;
    }

    private function getError(){
        $str="上传文件<font color='red'>{$this->originName}</font>时出错!";

        switch($this->errorNum){
            case 4:$str.="没有文件被上传!";break;
            case 3:$str.="文件只被部分上传!";break;
            case 2:$str.="上传文件超过了HTML表单中MAX_FILE_SIZE选项指定的值!";break;
            case 1:$str.="上传文件超过了php.ini中的upload_max_filesize选项的值!";break;
            case -1:$str.="未允许的类型";break;
            case -2:$str.="文件过大,上传文件不能超过{$thi->maxSize}个字节";break;
            case -3:$str.="上传失败";break;
            case -4:$str.="创建存放上传文件目录失败,请重新指定上传目录";break;
            case -5:$str.="必须指定上传文件的路径";break;
            default:$str.="未知错误";

        }
        return  $str."<br>";
    } 

    //用来检查文件上传路径
    private function checkFilePath(){
        if(empty($this->filePath)){
            $this->setOption('errorNum',-5);
            return false;
        }
        if(!file_exists($this->filePath)||!is_writable($this->filePath)){
            if(!@mkdir($this->filePath)){
                $this->setOption('errorNum',-4);
                return false;
            }
        }
        return true;
    }

    //用于检查文件上传的大小
    private function checkFileSize(){
        if($this->fileSize>$this->maxSize){
            $this->setOption('errorNum',-2);
            return false;
        }else{
        return true;
    }
}

    //用于检查文件上传类型
    private function checkFileType(){
        if(in_array($this->fileType,$this->allowType)){
            return true;
        }else{
            $this->setOption('errorNum',-1);
            return false;
        }
    }

    //设置上传后的文件名称
    private function setNewFileName(){
        if($this->isRandName){
            $this->setOption('newFileName',$this->proRandName());
        }else{
            $this->setOption('newFileName',$this->originName);
        }
    }

    //是否设置随机文件名
    private function proRandName(){
        $fileName=date("Y").date("m").date("d").date("H").date("i").date("s").rand(100,999).".";    //随机文件名
        return $fileName.".".$this->fileType;
    }


    //用来上传一个文件
    function uploadFile($fileFied){
        $return=true;
        //检查文件上传路径
        if(!$this->checkFilePath()){
            $this->errorMess=$this->getError();
            return false;
        }
        $name=$_FILES[$fileFied]['name'];
        $tmp_name=$_FILES[$fileFied]['tmp_name'];
        $size=$_FILES[$fileFied]['size'];
        $error=$_FILES[$fileFied]['error'];

        if($this->setFiles($name,$tmp_name,$size,$error)){
            //检查文件的尺寸和类型
            if($this->checkFileSize()&&$this->checkFileType()){
                $this->setNewFileName();
                if($this->copyFile()){
                    return true;
                }else{
                    $return=false;
                }
            }else{
                $return=false;
            }
        }else{
            $return=false;
        }
        if(!$return){
            $this->errorMess=$this->getError();
        }
        return $return;
    }

    //把上传的文件从临时文件中拷贝出来
    private function copyFile(){
        if($this->errorNum==0){
            //无论上传的路径是否有“/”都加上“/”
            $filePath=rtrim($this->filePath,'/').'/';
            $filePath.=$this->newFileName;

            if(@move_uploaded_file($this->tmpFileName, $filePath)){
                return true;
            }else{
                $this->setOption('errorNum',-3);
                return false;
            }
        }else{
            return false;
        }
    }

    //设置和$_FILES有关的内容
    private function setFiles($name="",$tmp_name="",$size=0,$error=0){
        $this->setOption('errorNum',$error);
        if($error){
            return false;
        }
        $this->setOption('originName',$name);
        $this->setOption('tmpFileName',$tmp_name);
        //分割
        $arrStr=explode('.', $name);
        $this->setOption('fileType',$arrStr[count($arrStr)-1]);
        $this->setOption('fileSize',$size);
        return true;
    }

    //用于获取上传后文件的文件名
    function getNewFileName(){
        return $this->newFileName;
    }

    //上传如果失败,则调用这个方法,就可以查看错误报告
    function getErrorMsg(){
        return $this->errorMess;
    }
}
?>