您的位置:首页 > 编程语言 > PHP开发

Thinkphp关于文件上传下载去重命名的解决办法

2018-03-29 08:07 513 查看
[php] view plain copy//实现上传的表单提交:    
    <form action="__URL__/upload" enctype="multipart/form-data" method="post" >    
       <input type="text" name="name" />    
       <input type="file" name="photo" />    
       <input type="submit" value="提交" >    
    </form>    
ThinkPHP文件上传操作使用Think\Upload类,假设前面的表单提交到当前控制器的upload方法,如下upload方法的实现代码:[html] view plain copy//文件上传  
[html] view plain copy  public function upload(){  
      if(IS_GET){  
          $this->display();  
      }else{  
      $upload = new \Think\Upload();// 实例化上传类  
      $upload->exts = array('csv');// 设置附件上传类型  
      $upload->rootPath = './Public/Upload/'; // 设置附件上传根目录  
      $upload->savePath = "";// 设置附件上传(子)目录  
            
      $info = $upload->upload();  
      if(!$info) {  
          // 上传错误提示错误信息  
        $this->error($upload->getError());      
      }else{  
          // 上传成功 获取上传文件信息  
          // $this->show('添加成功', 'utf8');   
        $this->import($upload->rootPath.$info['file']['savepath'].$info['file']['savename']);  
      }  
    }  
}  
  
  public function import($file){  
      // $file = "./Public/Upload/2018-03-21/5ab1b0ae7288f.csv";  
      $encoding = detect_encoding($file);  
  
      //如果不是utf8格式,转化为utf8格式  
      if($encoding !='UTF-8'){  
          $contents = file_get_contents($file);//将整个文件读入一个字符串   
          $contents = mb_convert_encoding($contents, "utf-8",$encoding);//转换字符的编码  
          file_put_contents($file, $contents);//将一个字符串写入文件 保存  
      }  
  
      $fp = fopen($file,'r'); //打开文件或者 URL   
[html] view plain copyif($fp){  
    $fields = array('no','name','sex');  
    $model = M('student');  
    //解决文件去重问题  
    $arr1 = $model->getField('no',true);  
  
    $arr = array();  
    while(($row = fgetcsv($fp,1000,",")) !== false){ //从文件指针中读入一行并解析 CSV 字段  
       $str = array_combine($fields, $row);//创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值  
     
      if(in_array($str['no'],$arr1)){//检查数组中是否存在某个值  
            echo $str['no'] . "已存在" . '<br>';  
        }else{  
            $arr1[] = $str['no'];  
            $arr[] = $str;  
            echo $str['no'] . "已导入" . '<br>';  
            // dump($arr);    
    }  
    if(count($arr)==1000){  
                $model->addAll($arr);  
                unset($arr);  
                }     
    }  
    if(count($arr)>0){  
        $model->addAll($arr);  
       }  
    }              
 $this->success('上传成功!');  
}  

tp下载文件程序如下[html] view plain copy/**  
    * 下载文件  
    * @param string $file  
    *               被下载文件的路径  
    * @param string $name  
    *               用户看到的文件名  
    */  
  public function download()  
  {  
          $session = $_SESSION['userName'];  
        if(empty($session)) $this->ajaxReturn('','未登录!',-1);  
       $file_name=$this->_post('subBox');  
       $file_dir=$this->_post('path');  
       if(empty($file_name)){  
            $this->error('没有选择文件');  
       }  
       if(!file_exists($file_dir.$file_name)){   //检查文件是否存在   
            $this->error('文件找不到');    
       }else{   
           $file = fopen($file_dir . $file_name,"r"); // 打开文件  
           // 输入文件标签  
           Header("Content-type: application/octet-stream");  
           Header("Accept-Ranges: bytes");  
           Header("Accept-Length: ".filesize($file_dir.$file_name));  
           Header("Content-Disposition: attachment; filename=".$file_name    );  
           // 输出文件内容  
           echo fread($file,filesize($file_dir . $file_name));  
           fclose($file);  
           exit();  
       }  
         
   }  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: