PHP的文件上传很好用,有没有类都无所谓

解决方案 »

  1.   

    直接贴出来排版不好调整,并且是固定了,放在blog以后还可以更新。
    我确实是在宣传我的blog,但诸位也用不着这么说三到四吧。(技术性的欢迎)
    把文件上传封在一起,总好过在每次上传时都要写一大段的重复代码了.并且我是php的入门学习者,写出来的肯定存在问题,大家尽可以对该类指出不足。谢谢。
      

  2.   

    <?php
    /*************************用法***********************************************************/
      /*<html>
      * <head>
      *   <title>文件上传实例</title>
      * </head>
      * <body>
      *  <form action="test.php" method="post" enctype="multipart/form-data">
      * <table border=0 cellPadding=3 cellSpacing=4 width=30%>
      *  <tr>
      *   <td width=10% nowrap>附件来源</td>
      *   <td><input name="src" type="file"/></td>
      *  </tr>
      *  <tr> 
      *   <td colSpan=2 align=center><input type="submit" value="上传"></td> 
      *  </tr>
      * </table>
      * </form>
      * </body>
      * </html>
      **********************end*********************************************
      * test.php @ 处理表单文件名
      * <?php 
      * 
      * require_once("../class/upload_file.php");
      *require_once("../config/config.inc.php");   *$type_id=$_POST[type_id];*if (!isset($_FILES['file']))
    *{
    * $f_upload = new UploadFile();
    * $f_upload->set_file_type($_FILES['file']['type']);
    * $f_upload->set_file_name($_FILES['file']['name']);
    * $f_upload->set_file_size($_FILES['flie']['size']);
    * $f_upload->set_upfile($_FILES['file']['tmp_name']);
    * $f_upload->set_base_dir($config_file_path."product_images/");
    * $f_upload->set_child_dir($config_file_path."product_images/".$type_id.'/');
    * $f_upload->save();
    }  *  
      * ?>
      *************************************end****************************************
    *************************************************************************************************/
    class UploadFile
    {
     //属性声明
     private $upfile_type;   //上传文件类型
     private $upfile_size;   //上传文件大小
     private $upfile_name;   //上传文件名称
     private $upfile;    //上传文件在服务端储存的临时文件名
     private $extention_list;  //允许上传文件类型
     private $size;     //最大允许上传的文件大小
     private $check;     //相符标记
     private $ext;     //以数组的形式获得分解后的允许上传的文件类型
     private $base_dir;    //文件存储根目录
     private $child_dir;    //文件存储子目录
     private $extention;    //文件扩展名
     private $file_path;    //完整访问路径
     private $url;      //文件上传成功后跳转路径
     
     /**构造函数**/
     function __construct()
     {
        $this->set_extention();             //初始化扩展名列表;
        $this->set_size(2048);              //初始化上传文件KB限制;
      
     }
     
     /**文件类型**/
     function set_file_type($upfile_type)
     {
       $this->upfile_type = $upfile_type;       //取得文件类型;
     } /**获得文件名**/
       function set_file_name($upfile_name)
     {
       $this->upfile_name = $upfile_name;       //取得文件名称;
     } /**获得文件**/
       function set_upfile($upfile)
     {
        $this->upfile = $upfile;            //取得文件在服务端储存的临时文件名;
     } /**获得文件大小**/
       function set_file_size($upfile_size)
       {
         $this->upfile_size = $upfile_size;       //取得文件大小;
       }   /**设置文件上传成功后跳转路径**/
       function set_url($url)
       {
         $this->url = $url;               //设置成功上传文件后的跳转路径;
       }   /**获得文件扩展名**/
       function get_extention()
       {
         $this->extention = preg_replace('/.*\.(.*[^\.].*)*/iU','\\1',$this->upfile_name); //取得文件扩展名;
       }   /**初始化允许上传文件类型**/
      function set_extention($extension="gif|jpg|jpeg|bmp|png")
      {
       $this->extention_list=$extension;
      }    /**设置最大上传KB限制**/
      function set_size($size)
      {
       $this->size = $size;              //设置最大允许上传的文件大小;
      }  /**初始化文件存储根目录**/
      function set_base_dir($directory)
      {
       $this->base_dir = $directory; //生成文件存储根目录;
      }
      
      /**初始化文件存储根目录**/
      function set_child_dir($directory)
      {
       $this->child_dir = $directory; //生成文件存储根目录;
      }
      
      /**错误处理**/
      function showerror($errstr="未知错误!")
      {
       echo "<script language=javascript>alert('$errstr');location='javascript:history.go(-1);';</script>";
       exit();
      }  /**跳转**/
      function go_to($str,$url)
      {
       echo "<script language='javascript'>alert('$str');location.href='$url'"</script>";
       exit();
      }  /**如果根目录没有创建则创建文件存储父目录**/
      function mk_base_dir()
      {
       if (! file_exists($this->base_dir)) //检测父目录是否存在;
       {   
        @mkdir($this->base_dir,0777);     //不存在则创建;
       }
      }
       
      /**如果根目录没有创建则创建文件存储子目录**/
      function mk_child_dir()
      {
       if (! file_exists($this->child_dir)) //检测子目录是否存在;
       {   
        @mkdir($this->child_dir,0777);     //不存在则创建;
       }
      }   /**以数组的形式获得分解后的允许上传的文件类型**/
      function get_compare_extention()
      {
       $this->ext = explode("|",$this->extention_list);//以"|"来分解默认扩展名;
      }  /**检测扩展名是否违规**/
      function check_extention()
      {
       for($i=0;each($this->ext);$i++)            //遍历数组;
       {
        if($this->ext[$i] == strtolower($this->extention)) //比较文件扩展名是否与默认允许的扩展名相符;
        {
         $this->check = true;               //相符则标记;
         break;
        }
       }
       if(!$this->check){$this->showerror("正确的扩展名必须为".$this->extention_list."其中的一种!");}
       //不符则警告
      }  /**检测文件大小是否超标**/
      function check_size()
      {
       if($this->upfile_size > round($this->size*1024))     //文件的大小是否超过了默认的尺寸;
       {
        $this->showerror("上传附件不得超过".$this->size."KB"); //超过则警告;
       }
      }  /**文件完整访问路径**/
      function set_file_path()
      {
       $this->file_path=$this->child_dir.$this->upfile_name;
      }  /**上传文件**/
      function copy_file()
      {
       if(move_uploaded_file($this->upfile,$this->file_path)){        //上传文件;
        print $this->go_to("文件已经成功上传!",$this->url);  //上传成功;
       }else {
        print $this->showerror("意外错误,请重试!");     //上传失败;
       }
      }  /**完成保存**/
      function save()
      {
       $this->get_extention();     //获得文件扩展名;
       $this->get_compare_extention(); //以"|"来分解默认扩展名;
       $this->check_extention();    //检测文件扩展名是否违规;
       $this->check_size();      //检测文件大小是否超限;   
       $this->mk_base_dir();        //如果根目录不存在则创建;
       $this->mk_child_dir();        //如果子目录不存在则创建;
       $this->set_file_path();     //生成文件完整访问路径;
       $this->copy_file();       //上传文件;
      }}
    ?>
      

  3.   

    封装不好,应该是这种接口:
    if (!isset($_FILES['file']))
    {
     $f_upload = new UploadFile('file');
     $f_upload->SetDestDir($config_file_path."product_images/");
     $f_upload->save();
    }
      

  4.   

    哎,copy过来的还是错的,应该是if(isset($_FILES['file']))
      

  5.   


    *if (isset($_FILES['file']))
    *{
    * $f_upload = new UploadFile();
    * $f_upload->set_file_type($_FILES['file']['type']);
    * $f_upload->set_file_name($_FILES['file']['name']);
    * $f_upload->set_file_size($_FILES['flie']['size']);
    * $f_upload->set_upfile($_FILES['file']['tmp_name']);
    * $f_upload->set_base_dir($config_file_path."product_images/");
    * $f_upload->set_child_dir($config_file_path."product_images/".$type_id.'/');
    * $f_upload->save();
    }  *
    感觉这里的操作太多了,一般只要设定一个上传的目录操作就可以了。
    其他的全在类中设置为默认操作。