龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

文件上传

时间:2014-07-22 14:51来源: 作者: 点击:
分享到:
<无详细内容>
<?
   /**
  *   上传类
  *   @author  firerat
  *   @email    lukai_rat@163.com
  *   @time     Feb 21  2011 10:00
  *   @lastmodify Feb 21  2011 10:00
  */
   class upload {
     private   $inputname ;
     private   $uploaddir;
     private   $allowtype;
     private   $logfile;
     private   $msg;

     //构造函数
     public  function __construct(){
            //
     }

     /**
      *  检查上传文件是否合法
      *  @param  $inputname
      *  @param  $allowsize  默认5M
      *  @param  $allowtype=array()
      *  @return array('success'=>$succes,'msg'=>)
      */
     function verifyfile($inputname,$allowsize= '5*1024*1024',$allowtype=array()){
       $obj = $_FILES[$inputname];

       //检查上传文件类型
       $type = strtolower(pathinfo($obj['name'],PATHINFO_EXTENSION));
       if(!in_array($type,$this->allowtype)){
            return   array(  'success' => false,
                             'msg' => $this->showmsg('upload_err_type_not_include')
                          );
       }

       //检查文件的大小
       $filesize = $obj['size'];
       if( $filesize > intval($this->allowsize)){
             return   array( 'success' => false,
                             'msg' => $this->showmsg('upload_err_size_too_large')
                           );
       }

       //文件只有部分被上传
       if($obj['error'] == 3){
            return   array( 'success' => false,
                            'msg' => $this->showmsg('upload_err_partial')
                           );
       }

       //没有文件被上传
       if($obj['error'] == 4){
            return   array( 'success' => false,
                             'msg' => $this->showmsg('upload_err_no_file')
                           );
      }

      //找不到临时文件夹
      if($obj['error'] == 6){
            return   array( 'success' => false,
                             'msg' => $this->showmsg('upload_err_no_tmp_dir')
                           );
      }

      //写入失败
      if($obj['error'] == 7){
            return   array( 'success' => false,
                             'msg' => $this->showmsg('upload_err_cant_write')
                           );
      }

      if($obj['error'] == 0){
            return   array( 'success' => true,
                             'msg' => $this->showmsg('ok')
                           );
      }else{
            return   array( 'success' => true,
                             'msg' => $this->showmsg('upload_err_other')
                           );
     }

    }

    /**
     *  上传核心操作
     *  @param  $inputname   上传的file的name
     *  @param  $allowsize   允许的文件的大小 B
     *  @param  $uploaddir   上传目录
     *  @param  $allowtype   允许类型
     *  @return  $newfile;   保存文件地址
     */
    public function start_upload($inputname=null,
                                     $allowsize=0,
                                        $uploaddir='upload',
                                             $allowtype=array('rar','zip')){
         //初始化对象
         $this->inputname = $inputname;
         $this->allowsize = intval($allowsize);
         $this->allowtype = $allowtype;
         $this->uploaddir = DIR_ABSOLUE_ROOT_PATH.'\\'.$uploaddir.'\\';
         $this->logfile = DIR_ABSOLUE_ROOT_PATH.'\\'.'upload.log';

         //上传file对象
         $obj = $_FILES[$inputname];

         //检查返回的参数
         $checkresult =  $this->verifyfile($this->inputname,$this->allowtype,$this->allowsize);

         //如果上传成功
         if(true == $checkresult['success']){
             //临时文件转移到目标文件夹
             $oldfile = $obj['tmp_name'];
             $ext = strtolower(pathinfo($obj['name'], PATHINFO_EXTENSION));
             $newsfilename = uniqid(md5(rand().time()), true);
             $newfile = $this->uploaddir.'\\'.$newsfilename.'.'.$ext;

             if(move_uploaded_file($oldfile, $newfile)){
                   return  $newfile;
             }else{
                  $this->write_err_msg();
             }
         }else{
              //记录上传失败消息
              $this->write_err_msg();
         }
    }


   /**
    *  显示错误信息
    *  @param $e
    *  @return $errormsg[$e]
    */
   private function  showmsg($e){
        $this->msg = $e;
        //错误消息初始化
        $errormsg = array(
                      'ok' => '上传成功' ,
                      'upload_err_partial' => '上传部分' ,
                      'upload_err_size_too_large' => '文件太大了' ,
                      'upload_err_no_file' => '文件没有被上传' ,
                      'upload_err_no_tmp_dir' => '没有找到临时文件夹',
                      'upload_err_cant_write' => '写入失败',
                      'upload_err_type_not_include' => '上传文件的类型不正确',
                      'upload_err_other' => '其他错误'
                     );

        //返回信息
        return $errormsg[$e];
    }

   /**
    * 将信息写入日志文件
    *
    *  @return 
    */
   private  function write_err_msg(){
        //具体实现
        $errormsg = $this->msg;
        $time = date('l dS \of F Y h:i:s A',time());
        $content =  '\r\n'.'      '.$time."   ||   ".$errormsg.'\n\r';
        $filename = $this->logfile;

        //写入日志文件,并启用追加到文件最后
        $len = file_put_contents($filename,$content,FILE_APPEND);
     }
   }

2. [文件] upload.class.php ~ 5KB     下载(34)     跳至 [1] [2] [3] [4] [全屏预览]

<?
   /**
  *   上传类
  *   @author  firerat
  *   @email    lukai_rat@163.com
  *   @time     Feb 16  2011 15:28
  *   @lastmodify Feb 16  2011 15:28
  */
   class upload {
     private   $inputname ;
     private   $uploaddir;
     private   $allowtype;
     private   $logfile;
     private   $msg;

     //构造函数
     public  function __construct(){
            //
     }

     /**
      *  检查上传文件是否合法
      *  @param  $inputname
      *  @param  $allowsize  默认5M
      *  @param  $allowtype=array()
      *  @return array('success'=>$succes,'msg'=>)
      */
     function verifyfile($inputname,$allowsize= '5*1024*1024',$allowtype=array()){
       $obj = $_FILES[$inputname];

       //检查上传文件类型
       $type = strtolower(pathinfo($obj['name'],PATHINFO_EXTENSION));
       if(!in_array($type,$this->allowtype)){
            return   array(  'success' => false,
                             'msg' => $this->showmsg('upload_err_type_not_include')
                          );
       }

       //检查文件的大小
       $filesize = $obj['size'];
       if( $filesize > intval($this->allowsize)){
             return   array( 'success' => false,
                             'msg' => $this->showmsg('upload_err_size_too_large')
                           );
       }

       //文件只有部分被上传
       if($obj['error'] == 3){
            return   array( 'success' => false,
                            'msg' => $this->showmsg('upload_err_partial')
                           );
       }

       //没有文件被上传
       if($obj['error'] == 4){
            return   array( 'success' => false,
                             'msg' => $this->showmsg('upload_err_no_file')
                           );
      }

      //找不到临时文件夹
      if($obj['error'] == 6){
            return   array( 'success' => false,
                             'msg' => $this->showmsg('upload_err_no_tmp_dir')
                           );
      }

      //写入失败
      if($obj['error'] == 7){
            return   array( 'success' => false,
                             'msg' => $this->showmsg('upload_err_cant_write')
                           );
      }

      if($obj['error'] == 0){
            return   array( 'success' => true,
                             'msg' => $this->showmsg('ok')
                           );
      }else{
            return   array( 'success' => true,
                             'msg' => $this->showmsg('upload_err_other')
                           );
     }

    }

    /**
     *  上传核心操作
     *  @param  $inputname   上传的file的name
     *  @param  $allowsize   允许的文件的大小 B
     *  @param  $uploaddir   上传目录
     *  @param  $allowtype   允许类型
     *  @return  $newfile;   保存文件地址
     */
    public function start_upload($inputname=null,$allowsize=0,$uploaddir='upload',$allowtype=array('rar','zip')){
         //初始化对象
         $this->inputname = $inputname;
         $this->allowsize = intval($allowsize);
         $this->allowtype = $allowtype;
         $this->uploaddir = DIR_ABSOLUE_ROOT_PATH.'\\'.$uploaddir.'\\';
         $this->logfile = DIR_ABSOLUE_ROOT_PATH.'\\'.'upload.log';

         //上传file对象
         $obj = $_FILES[$inputname];

         //检查返回的参数
         $checkresult = $this->verifyfile($this->inputname,$this->allowtype,$this->allowsize);

         //如果上传成功
         if(true == $checkresult['success']){
             //临时文件转移到目标文件夹
             $oldfile = $obj['tmp_name'];
             $ext = strtolower(pathinfo($obj['name'], PATHINFO_EXTENSION));
             $newsfilename = uniqid(md5(rand().time()), true);
             $newfile = $this->uploaddir.'\\'.$newsfilename.'.'.$ext;

             if(move_uploaded_file($oldfile, $newfile)){

                   return  $newfile;
             }else{
                  $this->write_err_msg();
             }
         }else{
              //记录上传失败消息
              $this->write_err_msg();
         }
    }


   /**
    *  显示错误信息
    *  @param $e
    *  @return $errormsg[$e]
    */
   private function  showmsg($e){
        $this->msg = $e;
        //错误消息初始化
        $errormsg = array(
                      'ok' => '上传成功' ,
                      'upload_err_partial' => '上传部分' ,
                      'upload_err_size_too_large' => '文件太大了' ,
                      'upload_err_no_file' => '文件没有被上传' ,
                      'upload_err_no_tmp_dir' => '没有找到临时文件夹',
                      'upload_err_cant_write' => '写入失败',
                      'upload_err_type_not_include' => '上传文件的类型不正确',
                      'upload_err_other' => '其他错误'
                     );

        //返回信息
        return $errormsg[$e];
    }

   /**
    * 将信息写入日志文件
    *
    *  @return 
    */
   private  function write_err_msg(){
        //具体实现
        $errormsg = $this->msg;
        $time = date('l dS \of F Y h:i:s A',time());
        $content =  '\r\n'.'      '.$time."   ||   ".$errormsg.'\n\r';
        $filename = $this->logfile;

        //写入日志文件,并启用追加到文件最后
        $len = file_put_contents($filename,$content,FILE_APPEND);
     }
   }

3. [文件] index.php ~ 403B     下载(24)     跳至 [1] [2] [3] [4] [全屏预览]

<?
   //定义该文件件的所在绝对路径
   define('DIR_ABSOLUE_ROOT_PATH',realpath(dirname(__FILE__)));
   include 'upload.class.php';

   $upload = new upload();
   $inputname = 'inputname';
   $allowsize =  1024; // 1G = 1024M  1M = 1024K  1K = 1024B
   $desdir = 'upload';
   $allowtype = array('rar','flv','zip');

   $upload->start_upload('file',$allowsize,'upload',array('rar','flv','zip'));

4. [文件] index.html ~ 562B     下载(26)     跳至 [1] [2] [3] [4] [全屏预览]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>上传表单</title>
</head>
<body>
    <form action="index.php" method="post"  enctype="multipart/form-data" >
        <p>
        <input type="file" name='file' />
        </p>
        <p>
        <input type="submit" name='submit' value='submit' />
        </p>
      </form>
</body>
</html>

5. [文件] 打包文件 ~ 2KB     下载(26)     [全屏预览]

精彩图集

赞助商链接