<?php
/**************************************************************************************
 ***  ContactmanageAction.class.php
 ***  说明:
 ***  日期:2012-08-12
 *************************************************************************************/
include './Public/Classes/PHPExcel/IOFactory.php';
//CountcarddataAction 控制类继承统一入口加载类 CommonAction
class ContactmanageAction extends  CommonAction {

  function index()
  {
     $contact_model=M('contact');
     $contactdata=$contact_model->where("userid=".$_SESSION['userid'])->select();
     //print_r($contactdata);
     //exit();
    // print_r($_SESSION);
       
    $this->assign('timer',$this->getTime());
    $this->assign('contactdata',$contactdata);
    $this->display();
  }

      #删除用户操作
      function delete()
      {
        if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest"){

             $contact_model=M('contact');
             $status = $contact_model->where('id='.$_POST['id'])->delete();

             if ($status)
             {

                  $data['status']='success';
                  $data['message']='删除常用联系人成功。';

             }
             else
             {
                  $data['status']='success';
                  $data['message']='删除常用联系人失败。';
             }      
              $this->ajaxReturn($data);      
           }
       }

  #跳转到添加
  function add()
  {
    $this->display();
  }

  #添常用联系人
  function addcontact()
  {   
    if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest"){
          

            $contact_model=M('contact');
            $telstatus=$this->check_telnum($_POST);

            if (empty($_POST['cname'])) {
               $data['status']="failed";
               $data['message']="添加失败。联系人不为空!";
               $this->ajaxReturn($data);
            }

           if (!$telstatus) {
             # code..
              $data['status']='failed';
              $data['message']="联系电话格式不正确";
              $this->ajaxReturn($data,'json');
           }

            $this->checkcname($_POST['cname']);
            $contactdata['telnum']=trim($_POST['telenum']);   
            $contactdata['cname']=trim($_POST['cname']);
            $contactdata['userid']=$_SESSION['userid'];      
            $addstatus = $contact_model->add($contactdata);

        if ($addstatus) {

            $data['status']="success";
            $data['message']="添加成功。";

        }else
        {
            $data['status']="failed";
            $data['message']="添加失败。";
        }

         $this->ajaxReturn($data,'json');
      }

  }

    function check_telnum($post)
     {     
            $telnum=$post['telenum'];
            $b1 = (preg_match("/^(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)$/i",$telnum))?TRUE:FALSE;
            $b2 = (preg_match("/^(1[358]{1}[0-9]{9})$/i",$telnum))?TRUE:FALSE;
            return $b1 || $b2;
     }

     function checkcname($cname)
     {
       $contact_model=M('contact');
       $adddata=$contact_model->where('cname='.$cname)->select();
           
       if (!empty($adddata)) {
            $data['status']="failed";
            $data['message']="添加失败。该常用联系人已存在!";
            $this->ajaxReturn($data);
         
       }
     }

      #初始化
      function update()
      {
        $id=$_GET['id'];
        $contact_model=M('contact');
        $contactdata=$contact_model->where('id='.$id)->select();
        $this->assign('contactdata',$contactdata);
        $this->display();   
      }
       

       function updatecontact()
       {
            if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest"){
              
                $contact_model=M('contact');
                $telstatus=$this->check_telnum($_POST);
                $id=$_POST['id'];

               if (!$telstatus) {
                 # code..
                  $data['status']='failed';
                  $data['message']="联系电话格式不正确";
                  $this->ajaxReturn($data,'json');
               }

                
                $contactdata['telnum']=trim($_POST['telenum']);   
            
               
                $addstatus = $contact_model->where('id='.$id)->save($contactdata);

            if ($addstatus) {

                $data['status']="success";
                $data['message']="修改成功。";

            }else
            {
                $data['status']="failed";
                $data['message']="修改失败。";
            }

             $this->ajaxReturn($data,'json');
          }
       }

       #批量导入常用联系人
       function bulkadd()
       {

           $this->display();
           
       }

       #导入实现
       function upfile()
       {
            
           $exceluptypes=array(
            'application/vnd.ms-excel',
            'application/vnd.ms-excel',
            'application/vnd.ms-excel',
            'application/x-excel',
            'application/vnd.ms-excel',
            'application/vnd.ms-excel',
            'application/vnd.ms-excel',
            'application/vnd.ms-excel',
            'application/kset',
               );
           $txtuptypes=array(
            'text/plain',
            );

          $destination_folder='./Public/file/';     
          if ($_SERVER['REQUEST_METHOD'] == 'POST')   //是否是post上传的文件
              {            
                 if (!is_uploaded_file($_FILES["file"][tmp_name]))
                //是否存在文件
                {
                  $this->assign('msgTitle','文件不存在');
                  $this->assign('message','请重试');   
                  $this->assign('jumpurl','__APP__/Contactmanage/bulkadd');                   
                  $this->display('jump');
                  exit();                         
                }
                else
                {
                     $file = $_FILES["file"];
                     if(!(in_array($file["type"], $exceluptypes)||in_array($file['type'], $txtuptypes)))
                    //检查文件类型
                      {
                      $this->assign('msgTitle','文件类型不符!仅可以上传EXCEL和TXT文本文档类型文件');
                      $this->assign('message','请重试');   
                      $this->assign('jumpurl','__APP__/Contactmanage/bulkadd');              
                      $this->display('jump');  
                      exit();  
                      }

                     
                    if(!file_exists($destination_folder))
                      {
                        mkdir($destination_folder);
                       
                      }      

                    $filename=$file["tmp_name"];//获取临时文件的名称 如果要是租服务器的话要改临时文件夹路径
                    $oldfilename=$_FILES["file"]['name'];//获取原来的excel文件名含有后缀               $pinfo=pathinfo($oldfilename);
                    $pinfo=pathinfo($oldfilename);
                    $ftype=$pinfo['extension'];// 获取文件的后缀名
                    $date=date('YmdGHis');   
                    $newfilename=$date.'.'.$ftype; // 新的文件路径
                    $destination = $destination_folder.$newfilename;  //新excel路径  

                     if ($ftype=="txt") {
                         $type=1;
                     }
                     else
                     {
                         $type=2;
                     }

                    if (file_exists($destination))
                    {
                      $this->assign('msgTitle','同名文件已经存在了');
                      $this->assign('message','请重试');  
                      $this->assign('jumpurl','__APP__/Contactmanage/bulkadd');     
                      $this->display('jump');
                      exit();
                    }

                    if(!move_uploaded_file ($filename, $destination))
                    {
                      $this->assign('msgTitle', '常用联系人导入出错请查看文件目录是否已满');
                      $this->assign('message', '请重试');
                      $this->assign('jumpurl', '__APP__/Contactmanage/bulkadd');   
                      $this->display('jump');
                      exit();
                    }

                    $this->adddata($destination,$type);// 把excel中的文件导入数据库                        
                }
              }     

       }

       #adddata
       function adddata($destination_folder,$type)
       {
             //修改权限

             chmod($destination_folder, 0777);
             if ($type!=1)
              {
                  $objPHPExcel = PHPExcel_IOFactory::load($destination_folder);       
                  $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);  

                  foreach ($sheetData as $key => $value) {
                        $data[$key]['cname'] =$value['A'];
                        $data[$key]['telnum'] =$value['B'];
                  }
                  /*print_r($data);
                  exit();*/
             }
             else
             {                
                  $content = file_get_contents($destination_folder);
                 //echo $content;          
                  $array = explode("\r\n", $content);
                  foreach ($array as $key => $value) {
                        $array[$key]=mb_convert_encoding($value, "UTF-8", "GBK");          
                  }
                  //echo '<pre>';
                  //print_r($array);
                  //exit();
                  $data=array();
                  foreach ($array as $key => $value) {

                            //$data[$key]['cname']=
                        $perarray=explode(" ",$value);
                        $data[$key]['cname']=$perarray[0];
                        $data[$key]['telnum']=$perarray[1];
                  }

             }

             $data=$this->checkfield($data);
             $checkresult=$data[1];
             $addresult=$this->bulkadddata($data[0]);
             $contact_model=M('contact');
             $contactdata=$contact_model->where("userid=".$_SESSION['userid'])->select();              
             $this->assign('timer',$this->getTime());
             $this->assign('contactdata',$contactdata);
             $this->assign('addresult',$addresult);
             $this->assign('checkresult',$checkresult);
             $this->display();

       }

       function bulkadddata($data)
       {
           $contact_model=M('contact');
           $addresult=array();
           foreach ($data as $key => $value) {
                 $value['userid']=$_SESSION['userid'];
                 $status=$contact_model->add($value);
                 if (!$status) {

                       $addresult[$key]="常用联系人".$value['cname']."添加失败";        
                  }
                  else
                  {
                       $addresult[$key]="常用联系人".$value['cname']."手机号码".$value['telnum']."添加成功";
                  }                         
           }

           return $addresult;
       }

 
       #验证手机号码格式

       //还需要验证是不是该批数据里面含有重复
       function checkfield($data)
       {      

            $data1=$this->getallcontact();    
            $contactdata =$data;
            foreach ($data as $key => $value) {
                    
 
                   $status=$this->check_telnum1($value['telnum']);                            
                   if (!$status) {                    
                          $returndata[$key]="常用联系人".$value['cname']."的手机号码不正确,请查证后再添加。";      
                          unset($contactdata[$key]);
                   }    
                   

                   if (in_array($value['cname'],$data1)) {                      
                            $returndata[$key]="常用联系人".$value['cname']."已存在,请尝试其他联系人姓名。";
                            unset($contactdata[$key]);   
                   }
            }
            $resultdata[0]=$contactdata;
            $resultdata[1]=$returndata;
            return $resultdata;
       }

# 手机号码验证实现
        function check_telnum1($telnum)
        {     
            
            $b1 = (preg_match("/^(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)$/i",$telnum))?TRUE:FALSE;
            $b2 = (preg_match("/^(1[358]{1}[0-9]{9})$/i",$telnum))?TRUE:FALSE;
            return $b1 || $b2;
        }

        #获取用户
        function getallcontact()
        {
          $contact_model=M('contact');
           return $contact_model->where('userid='.$_SESSION['userid'])->getfield('cname',true);

        }

        #下载
        function download()
        {
             $id=$_GET['id'];
             $file_name="批量添加联系人实例文档.";    
             if ($id==1) {
                  //txt
              $file_name=$file_name."txt";
        
             }
             else
             {
              $file_name=$file_name."xls";
             }  
                  $file_name=iconv("utf-8","gb2312",$file_name);
                  //$file_sub_path=$_SERVER['DOCUMENT_ROOT']."cardmanage/Public/downfile/";
                  $file_sub_path="./Public/downfile/";
                  $file_path=$file_sub_path.$file_name;
                  $fp=fopen($file_path,"r");
                  $file_size=filesize($file_path);
                  //下载文件需要用到的头
                  Header("Content-type: application/octet-stream");
                  Header("Accept-Ranges: bytes");
                  Header("Accept-Length:".$file_size);
                  Header("Content-Disposition: attachment; filename=".$file_name);
                  $buffer=1024;
                  $file_count=0;
                  while(!feof($fp) && $file_count<$file_size){
                      $file_con=fread($fp,$buffer);
                      $file_count+=$buffer;
                      echo $file_con;
                  }
                  fclose($fp);    
                  exit();

        }

   
}

thinkphp excel txt文件上传实现的更多相关文章

  1. Thinkphp 验证码、文件上传

    一.验证码 验证码参数 例题:登录时验证下验证码 LoginController.class.php <?php namespace Home\Controller; use Think\Con ...

  2. c# txt 文件上传、写入TXT文件、创建图形验证码

    asp.net mvc 图片上传 html 在使用包含文件上传控件的表单时,必须使用 enctype="multipart/form-data" 属性 <form encty ...

  3. 2014.12.13 ASP.NET文件上传

    一.文件上传:(一)上传到硬盘文件夹1.最简单的上传. [HTML代码] <asp:FileUpload ID="FileUpload1" runat="serve ...

  4. Ubuntu本地文件上传至HDFS文件系统出现的乱码问题及解决方案

    1.问题来源及原因 用shell命令上传到HDFS中之后出现中文乱码,在shell命令窗口查看如图: 在eclipse中的文件HDFS查看工具查看如图: 原因:上传至HDFS文件系统的文本文件(这里是 ...

  5. Angular 文件上传、下载

    1. 文件上传 本地可同时选择多个文件 将本地所选择的文件列出来 单个文件上传至服务器: 删除本地选择的文件 样式使用了bootstrap的样式 1. html - file.component.ht ...

  6. ubuntu中将本地文件上传到服务器

    (1)在本地的终端下,而不是在服务器上.在本地的终端上才能将本地的文件拷入服务器. (2) scp -r localfile.txt username@192.168.0.1:/home/userna ...

  7. Word,Excel,pdf,txt等文件上传并提取内容

    近期项目需求:1.要用到各种文件上传,下载. 2.并对文件进行搜索. 3.仅仅要文件里包括有搜索的内容,所有显示出来. 今天正好有时间整理一下,方便以后阅读,及对须要用到的朋友提供微薄之力.首先在实现 ...

  8. [SAP ABAP开发技术总结]文本文件、Excel文件上传下传

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. 文件上传下下载(不包含断点续传) Excel,Word导入导出基础

    1.文件上传下载(MVC应用) 视图:form表单,编码方式为multipart/form-data <body> <div> <form action="/D ...

随机推荐

  1. kail2 linux 安装vmware tools

    kali进去后,安装vmtools有点蛋疼,中途会问你要编译内核模块所需要的内核头文件,但是没有默认安装的.安装headers时又因为没有源下载不了,所以我们要做一些准备工作. 首先打开shell,我 ...

  2. 发现EF中字段错误

    在使用EF时,报错: 对一个或多个实体的验证失败.有关详细信息,请参见“EntityValidationErrors”属性 添加一个验证方法: 代码: using System; using Syst ...

  3. Ubuntu系统使用技巧

    ======================vbox 显示模式=====================right_ctrl+c     自动缩放right_ctrl_home  显示菜单====== ...

  4. JAVA并发,同步锁性能测试

    测试主要从运行时间差来体现,数据量越大,时间差越明显,例子如下: package com.xt.thinks21_2; /** * 同步锁性能测试 * * @author Administrator ...

  5. 深度探索QT窗口系统(五篇)

    窗口作为界面编程中最重要的部分,没有窗口就没有界面,是窗口让我们摆脱了DOS时代,按钮是一个窗口,文本框是一个窗口,标签页是一个窗口,...一个窗口可以由多个窗口组成,每天我们都在与窗口打交道,当你打 ...

  6. 注意:MainActivity的oncreate方法里不要再inflate布局了(MainActivity里的点击事件无响应)

    activity_main已经通过setContentView(R.layout.activity_main);设置给MainActivity, 不要再inflate出新布局,然后findviewby ...

  7. Markdown编辑器editor.md的使用---markdown上传图片

    http://kindeditor.org/ 确定下有没有查找替换功能 http://pandao.github.io/editor.md/ http://pandao.github.io/edito ...

  8. 老男孩 python学习自修第二十二天【文件上传与下载】

    1.使用socket实现文件上传 server.py #!/usr/bin/env python # _*_ coding:UTF-8 _*_ import os import SocketServe ...

  9. Python编程-从入门到实践 Eric Matthes 著 袁国忠 译 - - 第二章 动手试一试

    因为第一章的动手试一试基本都是探索性的,所以直接进入第二章. # 2.2 动手试一试 # 2_1 简单消息: 将一条消息存储到变量中,再将其打印出来. message = 'python 编程从入门到 ...

  10. 2019.01.19 bzoj3653: 谈笑风生(长链剖分优化dp)

    传送门 长链剖分优化dpdpdp水题. 题意简述:给一棵树,mmm次询问,每次给一个点aaa和一个值kkk,询问满足如下条件的三元组(a,b,c)(a,b,c)(a,b,c)的个数. a,b是c的祖先 ...