无法在CodeIgniter中读取Excel文件

时间:2016-07-18 07:31:58

标签: php excel codeigniter phpexcel

我在使用CodeIgniter3和PHPExcel库上传excel文件的项目中遇到问题。

我上传excel文件时出现问题,文件已成功上传到文件夹但无法读取。此外,它给出了一条错误消息:

  

加载文件“xls_file”时出错:无法打开文件./assets/xls_file/进行阅读。

因此数据无法保存到数据库中。

我希望系统读取xls_file文件夹内的文件,为什么系统将xls_file作为文件夹读取?我的朋友告诉我CodeIgniter 3中的URI可能有问题,但我们不知道如何解决问题的解决方案。这是我的控制器:

$fileName = time().$_FILES['file']['name'];
$config['upload_path'] = './assets/xls_file/'; //save excel file in folder
$config['file_name'] = $fileName;
$config['allowed_types'] = 'xls|xlsx|csv';
$config['max_size'] = 10000;

$this->load->library('upload');
$this->upload->initialize($config);

if(! $this->upload->do_upload('file') )
$this->upload->display_errors();

$media = $this->upload->data('file');
$inputFileName = './assets/xls_file/'.$media['file_name'];

try {
        $inputFileType = IOFactory::identify($inputFileName);
        $objReader = IOFactory::createReader($inputFileType);
        $objPHPExcel = $objReader->load($inputFileName);
    } catch(Exception $e) {
        die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
    }

    $sheet = $objPHPExcel->getSheet(0);
    $highestRow = $sheet->getHighestRow();
    $highestColumn = $sheet->getHighestColumn();

    for ($row = 2; $row <= $highestRow; $row++){                  //  Read a row of data into an array                 
        $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                                        NULL,
                                        TRUE,
                                        FALSE);

    //save into the column                              
         $data = array(
            "id_jalan"=> $rowData[0][0],
            "id_kriteria"=> $rowData[0][1],
            "nilai"=> $rowData[0][2],
            "tahun_anggaran"=> $rowData[0][3],
            "penanda"=> $rowData[0][4],
        );
        //save into the tabel
        $insert = $this->db->insert("value",$data);
        //delete_files($media['file_path']);

    }
redirect('page/landing');

0 个答案:

没有答案