美文网首页
文件上传

文件上传

作者: study34668 | 来源:发表于2018-10-27 12:27 被阅读0次
    <html>
      <head>
      <meta charset="utf-8">
      <title>文件/图片上传</title>
      </head>
      <body>
      <form action="upload_file.php" method="post" enctype="multipart/form-data">
          <label for="file">文件名:</label>
          <input type="file" name="file" id="file">
          <input type="submit" name="submit" value="提交">
      </form>
      </body>
      </html>
    

    上传的网页,效果如下


    提交给upload_file.php 以及它include的upload_class.php

    <?php
    require_once("upload.class.php");
    
    $n = $_FILES['file']['error'];
    if( $n > 0 )
    {
        if( $n == 1  or  $n == 2 )  
        {
            echo "PHP Error: 文件过大";
        }elseif( $n == 3 )
        {
            echo "PHP Error: 文件只有部分被上传";
        }elseif( $n == 4 )
        {
            echo "PHP Error: 没有文件被上传";
        }elseif( $n == 5 )
        {
            echo "PHP Error: 文件大小为0";
        }else
        {
            echo "PHP Error: 未知错误,文件上传失败";
        }
        exit;
    }
    
    $myFile = new Upload ( $_FILES["file"] );
    
    if( $myFile->user_limit() )
    {
        $a = $myFile->limit();
        if( $a < 0 )
        {
            if(  $json = $myFile->upload($a)  )
            {
                echo "文件上传成功!<br/>";
                echo $json; //由于规定上传成功后返回一个json
            }else
            {
                echo "文件上传失败,请检查文件是否正确";
            }
        }elseif( $a == 1 )
        {
            echo "文件太大,无法上传";
        }elseif( $a == 2 )
        {
            echo "文件类型错误";
        }elseif( $a == 3 )
        {
            echo "已存在该文件";
        }elseif( $a == 4 )
        {
            echo "查找合理文件类型失败";
        }else
        {
            echo "未知错误";
        }
    }else
    {
        echo "您上传的太频繁";
    }
    

    ···

    include "D:/Apache/Apache24/htdocs/Frame/core/common/function.php";
    
    class Upload
    {
        public $type;     //上传文件类型
        public $name;     //上传文件名
        //var $realFile; //这是保存文件的变量,提示,它可以直接由php提供的$_FILES变量赋值
        public $size;     //文件大小
        private $tmp;      //PHP临时文件地址
        
        private static $permit_type; //允许上传的文件类型
        
        const maxbite = 2*1024*1024; //定义最大字节数
        
        //允许的上传文件类型,实际上应该从数据库中读取,这里写死,请改用数据库
        /*static $permit_type = array(
            'files_type' => array(
                0 => 'zip',
                1 => 'rar'
            ),
            'pictures_type' => array(
                0 => 'jpg',
                1 => 'png',
                2 => 'gif'
            )
        );  使用数据库获取*/
    
    
        public function __construct( $realFile ) {
            //构造函数,用于创建一个上传请求的对象,应当验证上传文件的类型合法性
            $this->name = $realFile['name'];
            $i = strlen($this->name)-1;
            while( $this->name[$i] != '.') $i--;
            $this->name = substr($this->name, 0, $i+1);
            
            $t = explode('/', $realFile['type']);
            $this->type = $t[1];
            if( $this->type == 'jpeg' ) $this->type = 'jpg';
            
            $this->name = $this->name.$this->type;
            
            $this->size = $realFile['size'];
            
            $this->tmp = $realFile['tmp_name'];
        }
    

    ···
    为新的一次上传构造对象

    private static function get_permit_types(){
                //从数据库中获取所有允许的类型,主要用于赋值给静态成员变量$permit_type
                //返回值,成功返回true,失败返回false即可
                $mysqli = new mysqli("localhost", "root", "123456", "test");
                if($mysqli->connect_error)
                {
                    die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
                    return false;
                }
                if( !$result = $mysqli->query('select types from filetype') )
                {
                    return false;
                }
                $i = 0;
                while(  $filetype = $result->fetch_array(MYSQLI_ASSOC)  )
                {
                    self::$permit_type['files_type'][$i++] = $filetype['types'];
                }
                if( !$result = $mysqli->query('select types from picturetype') )
                {
                    return false;
                }
                $i = 0;
                while(  $picturetype = $result->fetch_array(MYSQLI_ASSOC)  )
                {
                    self::$permit_type['pictures_type'][$i++] = $picturetype['types'];
                }   
                $mysqli->close();
                return true;
                
                //echo "<pre/>";
                //print_r($permit_type);
            }
    
    
            public function limit(){
                //上传限制的方法,主要用于检测文件的各项合法(如大小),如果你能考虑到更多安全的因素(不仅是文件类型),那么更能体现你的NB,至于
                //返回值默认只要合法返回true,不合法返回false,如果想分类错误类型,那么请优秀的你自行修改我upload_file.php里的逻辑以便更好地报错
                
                // <0:可以上传(-1:文件 -2:图片)  1:文件太大  2:文件类型不合法  3:文件重名  4:查询合理类型失败
                
                if( $this->size > self::maxbite ) return 1;
                if( !self::get_permit_types() ) return 4;
                $name = $this->name;
                foreach( self::$permit_type['files_type'] as $value )
                {
                    if( $this->type == $value )
                    {
                        if( is_file("files/$name") )
                        {
                            return 3;
                        }else
                        {
                            return -1;
                        }
                    }
                }
                foreach( self::$permit_type['pictures_type'] as $value )
                {
                    if( $this->type == $value )
                    {
                        if( is_file("pictures/$name") )
                        {
                            return 3;
                        }else{
                            return -2;
                        }
                    }
                }
                return 2;
            }
    
            public function user_limit(){
                //对用户上传的权限进行限制,根据要求应当每个用户(你如果觉得麻烦可以把用户的识别特征写成一个常量,只要这个函数可以正常执行就行了)
                //返回值默认只要合法返回true,不合法返回false,如果想分类错误类型,那么请优秀的你自行修改我upload_file.php里的逻辑以便更好地报错
                
                if(  isset( $_COOKIE['next_time'] )  )
                {
                    if( $_COOKIE['next_time'] > time() )
                    {
                        return false;
                    }else{
                        return true;
                    }
                }else{
                    return true;
                }
            }
    
        }
    

    这些代码实现了对于上传的限制
    选择一个错误的文件格式上传



    显示格式错误



    再选一个正确的,但是已经被上传了


    这次是正确的,将执行upload

    public function upload( $n ){
                //对当前对象执行上传的操作,提示:上传后文件的信息至少应当存在数据库的某个表中,要求图片和其他类型的文件能被分类到files和pictures两个目录中,命名格式自行发挥
                //返回值要求上传失败返回false即可,上传成功可以返回一个文件存储信息的json
                if( $n == -1 ) $root_type = "files";
                if( $n == -2 ) $root_type = "pictures";
                $name = $this->name;
                $type = $this->type;
                $size = $this->size;
                if(  move_uploaded_file($this->tmp, "$root_type/$name")  )
                {
                    setcookie('next_time', time()+300, time()+300);
                    $mysqli = new mysqli("localhost", "root", "123456", "test");
                    if($mysqli->connect_error)
                    {
                        die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
                        return false;
                    }
                    $time = get_current_time();
                    if(  $mysqli->query("insert into upload_info values ('$name', '$type', $size, '$root_type/$name', '$time')")  )
                    {
                        $info = array($this->name, $this->type, $this->size, "$root_type/$name", $time);
                        if( $json = json_encode($info) )
                        {
                            return $json;
                        }else{
                            $json = 'Error: '.json_last_error();
                            return $json;
                        }
                    }else{
                        echo "文件已保存,数据库信息导入失败<br/>";
                        return false;
                    }
                }else{
                    return false;
                }           
            }
    

    返回一个json



    数据库中多了相应的信息



    马上再次上传

    文件上传的基本功能都完成了

    相关文章

      网友评论

          本文标题:文件上传

          本文链接:https://www.haomeiwen.com/subject/tltdtqtx.html