• PHP多文件批量上传类
    时间:2008-11-21   作者:佚名   出处:互联网

    <?php
    class uploadfile {
        /**
         * 该类支持多文件上传
         * 使用:初始化时参数$files的值为$_FILES数组,$dir值为上传文件新建目录
         * 通过setMaxSize($size)设定上传文件最大值,setType($type)设定上传文件类型,其中$type为字符串,如:gif|jpg|txt|doc
         * 主函数moveuploadfile(),上传到某一文件出错时显示错误信息并继续执行上传
         * @var unknown_type
         */
        private $_files;
        private $_ddir;  //设定文件上传后的目录,要求不含斜杠
        private $_type;  //上传文件类型数组
        private $_errors;
        private $_maxsize = 10240000;  //设定上传文件最大大小  默认10M  单位字节
        public  function __construct($files,$dir){
            $this->_files = $files;
            $dir = str_replace("/","",$dir);
            $this->_ddir = trim($dir);
        }
        /**
         * 设定上传文件的最大值
         * 单位byte
         *
         * @param 无返回值
         */
        public function setMaxSize($size){
            $this->_maxsize = $size;
        }
        /**
         * 限定上传文件的类型
         * $type必需为如:gif|jpg|pdf|txt 等一系列由|及文件后缀名连接的字符串
         * @param 无返回值
         */
        public function setType($type){
            $this->_type = explode("|",$type);
        }
        /**
         * 获取文件的后缀名
         * @param $type=$file["type"]
         * @return $suffix
         */
        public function getSuffix($name){
            $pos = strpos($name,".")+1;
            $suffix = substr($name,$pos);
            return $suffix;
        }
        /**
         * 检查错误
         * @return 错误信息
         */
        public function checkError($file){
                if ($file[1]["size"]>$this->_maxsize) {
                    $this->_errors[] = "Too large the file is!";
                    return false;
                }
                if (!in_array($this->getSuffix($file[1]["name"]),$this->_type)) {
                    $this->_errors[] = "Type of the file is wrong!";
                    return false;
                }
                switch ($file[1]["error"]){
                    case 0: {$this->_errors[] = "";return true;break;}
                    case 1: {$this->_errors[] = "Length of the file is out of the value of upload_max_filesize!";return false;break;}
                    case 2: {$this->_errors[] = "Length of the file is out of the value of MAX_FILE_SIZE!";return false;break;}
                    case 3: {$this->_errors[] = "Upload has been broken up!";return false;break;}
                    case 4: {$this->_errors[] = "The upload file is null!";return false;break;}
                    default: {$this->_errors[] = "Other reason!";return false;}
                }
                return true;
        }
        /**
         * 上传主函数
         * @author guofang
         * @date 08/26/2008
         * @return 出错时显示错误信息
         *
         */
        public function moveuploadfile(){
            if (!is_dir(realpath(".")."\\".$this->_ddir)) {
                mkdir(realpath(".")."\\".$this->_ddir);
            }
            while ($file = each($this->_files)) {
                if (empty($file[1]["name"])) {
                    continue;
                }
                if (!$this->checkError($file)||!move_uploaded_file($file[1]["tmp_name"],realpath(".")."\\".$this->_ddir."\\".$file[1]["name"])) {
                    foreach ($this->_errors as $error){
                        echo "<br>Failed:<font color=red>$error</font>";
                    }
                    continue;
                }
            }
            return "Complete!";
        }
    }

    网友留言/评论

    我要留言/评论