文件上传

用户页面




文件/图片上传





注意:重命名不会修改文件后缀,如果不想重命名可以不填。

上传文件的类

realFile = $realFile;
        $this->name = $name;
        $this->size = $size;
        $this->type;
    }

    private static function get_permit_types()
    {
        $mysqli = new mysqli('localhost', 'root','', 'upload');
        if (!$mysqli)
        {
            die("Connection Failed " . $mysqli->connect_error);
        }
        $res1 = $mysqli->query('select allow_types from filetypes');
        $res2 = $mysqli->query('select pic_allow_types from pictypes');
        $i = 0;
        while($row = $res1->fetch_array(MYSQLI_ASSOC))
        {
            self::$files_types[$i++] = $row['allow_types'];
        }
        while($row = $res2->fetch_array(MYSQLI_ASSOC))
        {
            self::$pictures_types[$i++] = $row['pic_allow_types'];
        }
        if(isset(self::$files_types) and isset(self::$pictures_types))
        {
            $res1->free();
            $res2->free();
            $mysqli->close();
            return true;
        }
        else
        {
            $res1->free();
            $res2->free();
            $mysqli->close();
            return false;
        }
    }

    public function add_type(){
        $mysqli = new mysqli('localhost', 'root','', 'upload');
        if (!$mysqli)
        {
            die("Connection Failed " . $mysqli->connect_error);
        }

        $sql1 = "insert into filetypes(allow_types) VALUES ('')";
        $sql2 = "insert into pictypes(pic_allow_types) VALUES ('')";

        if($mysqli->query($sql1) or $mysqli->query($sql2))
        {
            $mysqli->close();
            return true;
        }
        else
        {
            $mysqli->close();
            return false;
        }
    }

    public function delete_type(){
        //删除可上传类型的函数,原则上从数据库中删除这一条
        //返回值,成功返回true,失败返回false即可
        $mysqli = new mysqli('localhost', 'root','', 'upload');
        $sql1 = "delete from filetypes where allow_types =''";
        $sql2 = "delete from pictypes where pic_allow_types =''";
        if($mysqli->query($sql1) or $mysqli->query($sql2))
        {
            $mysqli->close();
            return true;
        }
        else
        {
            $mysqli->close();
            return false;
        }
    }

    public function upload(){
        //对当前对象执行上传的操作,提示:上传后文件的信息至少应当存在数据库的某个表中,要求图片和其他类型的文件能被分类到files和pictures两个目录中,命名格式自行发挥
        //返回值要求上传失败返回false即可,上传成功可以返回一个文件存储信息的json
        $mysqli = new mysqli('localhost', 'root','', 'upload');
        if (!$mysqli)
        {
            die("Connection Failed " . $mysqli->connect_error);
        }

        if ($this->realFile["error"] > 0)
        {
            echo "错误: " . $this->realFile["error"] . "
"; return false; } else { self::get_permit_types(); $temp = explode('.', $this->name); $extension = end($temp); if(in_array($extension, self::$files_types)) { move_uploaded_file($this->realFile["tmp_name"], "D:/Project/homework/files/" . $this->name); $name = $this->name; $type = $this->realFile['type']; $size = $this->size; $path = 'D:/Project/homework/files'; $sql3 = "insert into files(filename, type, size, path) values('$name', '$type', '$size', '$path')"; if(!$mysqli->query($sql3)) { return false; } } if(in_array($extension, self::$pictures_types)) { move_uploaded_file($this->realFile["tmp_name"], "D:/Project/homework/pictures/" . $this->name); $name = $this->name; $type = $this->realFile['type']; $size = $this->size; $path = 'D:/Project/homework/pictures'; $sql3 = "insert into pics(filename, type, size, path) values('$name', '$type', '$size', '$path')"; if(!$mysqli->query($sql3)) { return false; } } echo "上传文件名: " . $this->name . "
"; echo "文件类型: " . $this->realFile['type'] . "
"; echo "文件大小: " . $this->size . "
"; echo "文件临时存储目录: " . $this->realFile['tmp_name'] . "
"; echo "文件所在目录" . 'D:/Project/homework/pictures/' . $this->name . "
"; $result = array( 'name' => $this->name, 'type' => $this->realFile['type'], 'size' => $this->realFile['size'], 'temp' => $this->realFile['tmp_name'] ); setcookie('upload_file','upload_file', time()+300); return json_encode($result); } } public function limit(){ //上传限制的方法,主要用于检测文件的各项合法(如大小),如果你能考虑到更多安全的因素(不仅是文件类型),那么更能体现你的NB,至于 //返回值默认只要合法返回true,不合法返回false,如果想分类错误类型,那么请优秀的你自行修改我upload_file.php里的逻辑以便更好地报错 self::get_permit_types(); // 从数据库获取允许的类型 $temp = explode('.', $this->name); $extension = end($temp); if($this->size > self::$maxSize) { return false; } elseif(!in_array($extension, self::$files_types) and !in_array($extension, self::$pictures_types)) { return false; } else { return true; } } public function user_limit(){ //对用户上传的权限进行限制,根据要求应当每个用户(你如果觉得麻烦可以把用户的识别特征写成一个常量,只要这个函数可以正常执行就行了) //返回值默认只要合法返回true,不合法返回false,如果想分类错误类型,那么请优秀的你自行修改我upload_file.php里的逻辑以便更好地报错 if(!isset($_COOKIE['upload_file'])) { return true; } if(isset($_COOKIE['upload_file'])) { return false; } } public function rename($new_name){ //修改上传文件名的方法,传入name则改名,不传则不改名 //改名返回true,未修改返回false if($new_name != '') { $temp = explode('.', $this->realFile['name']); $extension = end($temp); $this->name = $new_name . '.' . $extension; return true; } if($new_name == '') { return false; } } }

上传文件的脚本。

rename($_POST["new_name"]);//尝试重命名
if($myFile->limit()){
    if($myFile->user_limit()){
        if($result = $myFile->upload()){
            echo "文件上传成功!" . "
"; echo $result;//由于规定上传成功后返回一个json }else{ echo "文件上传失败!"; } }else{ echo "上传频率过快!请5分钟后再试"; } }else{ echo "文件不合法!"; }

实际效果

正常上传
连续上传
修改文件名
修改后页面
实际修改效果
数据库1
数据库2

你可能感兴趣的:(文件上传)