php无法读取.xlsx文件

时间:2016-05-04 08:04:02

标签: php excel codeigniter

我有一个上传excel .xls格式的表单,它运行正常但是 当我尝试将允许的类型更改为' .xlsx'我收到错误:

文件名lalala.xlsx不可读..

这是我的控制者:

function pesan()
    {
        $data= array( );
        if($this->input->post())
        {
            $email    =   $this->input->post('email');
            $config['upload_path'] = './kirim_undangan/';
            $config['allowed_types'] = 'xlsx';


            $this->load->library('upload', $config);
            $this->load->library('session');
            $this->session->xls;
            $data = $_SESSION['xlsx'];

            if ( !isset ($this->session->xls))//session uniq id isset ?
            {
                $data = array('error' => $this->upload->display_errors());

            }
            else
            {
                $data = array('error' => false);
                //$upload_data = $this->upload->data();

                $this->load->library('excel_reader');
                $this->excel_reader->setOutputEncoding('CP1251');

                $file =  FCPATH.'/kirim_undangan/' . $_SESSION['xlsx'] ; //pathnya file excel + uniqid + .xls
                $this->excel_reader->read($file);
                error_reporting(E_ALL ^ E_NOTICE);

                // Sheet 1
                $regex = "~\d{5}~";

                $data = $this->excel_reader->sheets[0] ;
                            $dataexcel = Array();
                for ($i = 1; $i <= $data['numRows']; $i++) 
                {

                    if($data['cells'][$i][1] == '') break;
                    $dataexcel[$i-1]['to_name'] = $data['cells'][$i][1];
                    $dataexcel[$i-1]['to_address'] = $data['cells'][$i][2];
                    $dataexcel[$i-1]['to_phone'] = $data['cells'][$i][3];
                    preg_match($regex, $data['cells'][$i][2] , $result);
                    $dataexcel[$i-1]['to_zipcode'] = $result[0];
                }

            $data_user = array(

                'request_Id'                    => $this->input->post('id'),
                'email'                         => $this->input->post('email'),
                'name'                          => $this->input->post('from_nama'),
                'phone'                         => $this->input->post('from_phone'),
                'status'                        => '0',
                'unique_id'                     => uniqid()


                );


            unlink(FCPATH.'/kirim_undangan/' . $_SESSION['xlsx']);
            $this->load->model('excel');
            $this->excel->tambahuser($dataexcel,$data_user);
            $data['msg'] = "Terima kasih ! Silahkan tunggu konfirmasi biaya melalui email !";

            }

        } 
        $this->load->view('index.php',$data);
    } 

    function upload() 
    {
        $this->load->library('session');

        if (!empty($_FILES)) 
        {
            $tempFile = $_FILES['file']['tmp_name'];
            $data = uniqid(). '.xlsx';
            $targetPath = getcwd() . '/kirim_undangan/';
            $targetFile = $targetPath . $data ;

            move_uploaded_file($tempFile, $targetFile);
            $_SESSION["xlsx"] = $data;
            print_r($_SESSION['xlsx']);
        }
    }

解决我问题的最佳方法是什么? CI库是否有任何方法可以识别xlsx文件?

我发现PHPExcel适合我的问题,但我不知道如何使用它。

0 个答案:

没有答案