找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1652|回复: 0

[教程] 一个简单PHP上传文件的类

[复制链接]
发表于 2013-3-31 19:42:20 | 显示全部楼层 |阅读模式 来自 广东省湛江市
  1. <?php
  2.    /**
  3.    * PHP100.com - 个人感觉非常简单,只要有点PHP基础滴人都应该能看懂~~
  4.    * Apache2 + PHP5.0
  5.    * Version:1.0
  6.    * 同时感谢PHP100所有的兄弟们
  7.    * ————————————————————————————————— Example —
  8.    * test.php @ 处理表单文件名
  9.    * <?php
  10.   *
  11.   * include('upload.php');                 # 加入类文件
  12.    * $f_upload = new upload_other;             # 创建对象
  13.    * $f_upload->set_file_type($_FILES['src']['type']);   # 获得文件类型
  14.    * $f_upload->set_file_name($_FILES['src']['name']);   # 获得文件名称
  15.    * $f_upload->set_file_size($_FILES['src']['size']);   # 获得文件尺寸
  16.    * $f_upload->set_upfile($_FILES['src']['tmp_name']);  # 服务端储存的临时文件名
  17.    * $f_upload->set_size(100);               # 设置最大上传KB数
  18.    *  $f_upload->set_base_directory('uploadImages');    # 文件存储根目录名称
  19.    * $f_upload->set_url('up.php');             # 文件上传成功后跳转的文件
  20.    * $f_upload->save();                  # 保存文件
  21.    *  
  22.    * ?>
  23.    * —————————————————————————————————— End —
  24.    *
  25.   * 偶没有在类里做有无文件的验证,大家可以在前台搞定~~
  26.    *
  27.    * —————————————————————————— Form —
  28.    * upload.htm @ 表单文件名
  29.    *
  30.   * <html>
  31.    * <head>
  32.    *   <title>文件上传实例</title>
  33.    * </head>
  34.    * <body>
  35.    *  <form action='test.php' method='post' enctype='multipart/form-data'>
  36.    * <table border=0 cellPadding=3 cellSpacing=4 width=30%>
  37.    *  <tr>
  38.    *   <td width=10% nowrap>附件来源</td>
  39.    *   <td><input name='src' type='file'/></td>
  40.    *  </tr>
  41.    *  <tr>
  42.   *   <td colSpan=2 align=center><input type='submit' value='上传'></td>
  43.   *  </tr>
  44.    * </table>
  45.    * </form>
  46.    * </body>
  47.    * </html>
  48.    *
  49.   * ————————————————————————————  End  —
  50.    *
  51.    * 上面的两个文件可根据自己的需要决定合并或不合并~~如果在使用中有任何问题可以跟偶联系~~^_^
  52.    *
  53. **/
  54.    /**文件上传类**/

  55. class upload_file
  56. {
  57.    /**声明**/
  58.    var $upfile_type,$upfile_size,$upfile_name,$upfile;
  59.    var $d_alt,$extention_list,$tmp,$arri;
  60.    var $datetime,$date;
  61.    var $filestr,$size,$ext,$check;
  62.    var $flash_directory,$extention,$file_path,$base_directory;
  63.   var $url; //文件上传成功后跳转路径;

  64.    function upload_file()
  65.    {
  66.     /**构造函数**/
  67.     $this->set_url('index.php');          //初始化上传成功后跳转路径;
  68.     $this->set_extention();             //初始化扩展名列表;
  69.     $this->set_size(50);              //初始化上传文件KB限制;
  70.     $this->set_date();               //设置目录名称;
  71.     $this->set_datetime();             //设置文件名称前缀;
  72.     $this->set_base_directory('attachmentFile');  //初始化文件上传根目录名,可修改!;
  73.    }
  74.    
  75.    /**文件类型**/
  76.    function set_file_type($upfile_type)
  77.    {
  78.     $this->upfile_type = $upfile_type;       //取得文件类型;
  79.    }
  80.    
  81.    /**获得文件名**/
  82.    function set_file_name($upfile_name)
  83.    {
  84.     $this->upfile_name = $upfile_name;       //取得文件名称;
  85.    }
  86.    
  87.    /**获得文件**/
  88.    function set_upfile($upfile)
  89.    {
  90.     $this->upfile = $upfile;            //取得文件在服务端储存的临时文件名;
  91.    }
  92.      
  93.   /**获得文件大小**/
  94.    function set_file_size($upfile_size)
  95.    {
  96.     $this->upfile_size = $upfile_size;       //取得文件尺寸;
  97.    }
  98.    
  99.    /**设置文件上传成功后跳转路径**/
  100.    function set_url($url)
  101.    {
  102.     $this->url = $url;               //设置成功上传文件后的跳转路径;
  103.    }
  104.    
  105.    /**获得文件扩展名**/
  106.    function get_extention()
  107.    {
  108.      $this->extention = preg_replace('/.*.(.*[^.].*)*/iU','\1',$this->upfile_name); //取得文件扩展名;
  109.    }
  110.       
  111.    /**设置文件名称**/
  112.    function set_datetime()
  113.    {
  114.     $this->datetime = date('YmdHis');        //按时间生成文件名;
  115.    }
  116.    
  117.    /**设置目录名称**/
  118.    function set_date()
  119.    {
  120.     $this->date = date('Y-m-d');          //按日期生成目录名称;
  121.    }
  122.    
  123.    /**初始化允许上传文件类型**/
  124.    function set_extention()
  125.    {
  126.     $this->extention_list = 'doc|xls|ppt|avi|txt|gif|jpg|jpeg|bmp|png'; //默认允许上传的扩展名称;
  127.    }  
  128.    
  129.    /**设置最大上传KB限制**/
  130.    function set_size($size)
  131.    {
  132.     $this->size = $size;              //设置最大允许上传的文件大小;
  133.    }
  134.    
  135.    /**初始化文件存储根目录**/
  136.    function set_base_directory($directory)
  137.    {
  138.     $this->base_directory = $directory; //生成文件存储根目录;
  139.    }
  140.    
  141.    /**初始化文件存储子目录**/
  142.    function set_flash_directory()
  143.    {
  144.     $this->flash_directory = $this->base_directory.'/'.$this->date; //生成文件存储子目录;
  145.    }
  146.    
  147.    /**错误处理**/
  148.    function showerror($errstr='未知错误!'){
  149.     echo '<script language=javascript>alert('$errstr');location='javascript:history.go(-1);';</script>';
  150.     exit();
  151.    }
  152.    
  153.    /**跳转**/
  154.    function go_to($str,$url)
  155.    {
  156.     echo '<script language='javascript'>alert('$str');location='$url';</script>';
  157.     exit();
  158.    }
  159.     /**如果根目录没有创建则创建文件存储目录**/
  160.    function mk_base_dir()
  161.    {
  162.     if (! file_exists($this->base_directory)){   //检测根目录是否存在;
  163.      @mkdir($this->base_directory,0777);     //不存在则创建;
  164.     }
  165.    }
  166.     /**如果子目录没有创建则创建文件存储目录**/
  167.    function mk_dir()
  168.    {
  169.     if (! file_exists($this->flash_directory)){   //检测子目录是否存在;
  170.      @mkdir($this->flash_directory,0777);     //不存在则创建;
  171.     }
  172.    }  
  173.    
  174.    /**以数组的形式获得分解后的允许上传的文件类型**/
  175.    function get_compare_extention()
  176.    {
  177.     $this->ext = explode('|',$this->extention_list);//以'|'来分解默认扩展名;
  178.    }
  179.    
  180.    /**检测扩展名是否违规**/
  181.    function check_extention()
  182.    {
  183.     for($i=0;each($this->ext);$i++)            //遍历数组;
  184.     {
  185.      if($this->ext[$i] == strtolower($this->extention)) //比较文件扩展名是否与默认允许的扩展名相符;
  186.      {
  187.       $this->check = true;               //相符则标记;
  188.       break;
  189.      }
  190.     }
  191.     if(!$this->check){$this->showerror('正确的扩展名必须为'.$this->extention_list.'其中的一种!');}
  192.     //不符则警告
  193.    }
  194.    
  195.    /**检测文件大小是否超标**/
  196.    function check_size()
  197.    {
  198.     if($this->upfile_size > round($this->size*1024))     //文件的大小是否超过了默认的尺寸;
  199.     {
  200.      $this->showerror('上传附件不得超过'.$this->size.'KB'); //超过则警告;
  201.     }
  202.    }
  203.     /**文件完整访问路径**/
  204.    function set_file_path()
  205.    {
  206.     $this->file_path = $this->flash_directory.'/'.$this->datetime.'.'.$this->extention; //生成文件完整访问路径;
  207.    }
  208.    
  209.    /**上传文件**/
  210.    function copy_file()
  211.    {
  212.     if(copy($this->upfile,$this->file_path)){        //上传文件;
  213.      print $this->go_to('文件已经成功上传!',$this->url);  //上传成功;
  214.     }else {
  215.      print $this->showerror('意外错误,请重试!');     //上传失败;
  216.     }
  217.    }
  218.    
  219.    /**完成保存**/
  220.    function save()
  221.    {
  222.     $this->set_flash_directory();  //初始化文件上传子目录名;
  223.     $this->get_extention();     //获得文件扩展名;
  224.     $this->get_compare_extention(); //以'|'来分解默认扩展名;
  225.     $this->check_extention();    //检测文件扩展名是否违规;
  226.     $this->check_size();      //检测文件大小是否超限;   
  227.     $this->mk_base_dir();      //如果根目录不存在则创建;
  228.     $this->mk_dir();        //如果子目录不存在则创建;
  229.     $this->set_file_path();     //生成文件完整访问路径;
  230.     $this->copy_file();       //上传文件;
  231.    }
  232.    
  233. }
  234. ?>
复制代码

发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如何回报帮助你解决问题的坛友,好办法就是点击帖子下方的评分按钮给对方加【金币】不会扣除自己的积分,做一个热心并受欢迎的人!

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 需要先绑定手机号

关闭

站长推荐上一条 /1 下一条

QQ|侵权投诉|广告报价|手机版|小黑屋|西部数码代理|飘仙建站论坛 ( 豫ICP备2022021143号-1 )

GMT+8, 2024-5-2 08:43 , Processed in 0.045349 second(s), 8 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表