【问题标题】:codeigniter phpexcel error ZipArchive::getFromName(): Invalid or uninitialized Zip objectcodeigniter phpexcel 错误 ZipArchive::getFromName(): Invalid or uninitialized Zip object
【发布时间】:2018-06-30 20:02:03
【问题描述】:

我正在尝试使用 codeigniter 和 phpexcel 将数据从 excel 文件 (.xlsx) 导入到 oracle,这是我的控制器:

private $filename;
public function form(){
    $data = array(); 
    if(isset($_POST['preview'])){ 
        $upload = $this->RoadmapModel->upload_file($this->filename);
        $upload_data = $this->upload->data();
        $this->filename = $upload_data['file_name'];
        
        if($upload['result'] == "success"){ 
            include APPPATH.'third_party/PHPExcel/PHPExcel.php';
            $excelreader = new PHPExcel_Reader_Excel2007();
            $loadexcel = $excelreader->load('excel/'.$this->filename); 
            $sheet = $loadexcel->getActiveSheet()->toArray(null, true, true ,true);
            $data['sheet'] = $sheet; 
           }else{ // Jika proses upload gagal
            $data['upload_error'] = $upload['error'];
        }
    }       
    $this->load->view('form', $data);
}

public function import(){
    include APPPATH.'third_party/PHPExcel/PHPExcel.php';        
    $excelreader = new PHPExcel_Reader_Excel2007();
    $loadexcel = $excelreader->load('excel/'.$this->filename = $this -> form()); 
    $sheet = $loadexcel->getActiveSheet()->toArray(null, true, true ,true);
    $data = [];
    $numrow = 1;
    foreach($sheet as $row){
        if($numrow > 1){
            // Kita push (add) array data ke variabel data
            array_push($data, [
                'TAHUN'=>$row['A'], 
                'PROVINCEID'=>$row['B'], 
                'PROVINSI'=>$row['C'], 
                'PLAN_DESAB'=>$row['D'], 
                'ACTUAL_DESAB'=>$row['E'],
                'PLAN_ELEKTRIFIKASI'=>$row['F'],
                'ACTUAL_ELEKTRIFIKASI'=>$row['G'],
                'PLAN_LISDES'=>$row['H'],
                'ACTUAL_LISDES'=>$row['I'],
            ]);
        }           
        $numrow++;
    }
    $this->RoadmapModel->insert_multiple($data);
    redirect("Roadmap"); 
}

这是我的模型:

public $tablename = "X";
function upload_file($filename){
    $this->load->library('upload'); 
    
    $config['upload_path'] = './excel/';
    $config['allowed_types'] = 'xlsx';
    $config['max_size'] = '2048';
    $config['overwrite'] = true;
    $config['file_name'] = $filename;

    $this->upload->initialize($config); 
    if($this->upload->do_upload('file')){ 
        $return = array('result' => 'success', 'file' => $upload_data = $this->upload->data(), 'error' => '');
        return $return;
    }else{
        $return = array('result' => 'failed', 'file' => '', 'error' => $this->upload->display_errors());
        return $return;
    }
}

function insert_multiple($data){
    $p_tablename= $this->tablename;
    $this->db->insert_batch($p_tablename, $data);
}

当我使用导入功能时,这是错误消息:

消息:ZipArchive::getFromName():Zip 对象无效或未初始化

文件名:Reader/Excel2007.php

行号:327

回溯:

文件:C:\xampp\htdocs\web_excel_ci\application\controllers\Roadmap.php

行:82

功能:加载

line : 82 is $loadexcel = $excelreader->load('excel/'.$this->filename = $this -> form()); in function import()

用于加载要导入的 excel 文件,我尝试使用 $this->filename = $this->form() 从函数 form() 中获取文件名,但这会出错

请帮我解决问题,因为我已经在那里堆叠了

非常感谢...

【问题讨论】:

    标签: php oracle codeigniter phpexcel phpexcelreader


    【解决方案1】:

    From the Docs

    默认情况下,PHPWord 使用 Zip 扩展名来处理 ZIP 压缩档案和其中的文件。如果您无法在服务器上安装 Zip 扩展,您可以使用纯 PHP 库替代方案 PclZip,它包含在 PHPWord 中。

    \PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP);
    

    这对我有用。

    【讨论】:

      【解决方案2】:

      在这个堆栈溢出中搜索了很多小时后,我得到了解决这个问题的解决方案,

      我使用$this->session->set_flashdata('fileName',$fileName); 在一个函数中获取值

      并使用$fileName = $this->session->flashdata('fileName'); 将该值放入另一个函数中

      并且成功了。

      感谢大家的关注...

      【讨论】:

        【解决方案3】:

        线路肯定有问题

        $loadexcel = $excelreader->load('excel/'.$this->filename = $this->form());
        

        它似乎试图将$this->filename 设置为$this->form() 的返回值。但是$this->form() 没有返回值 - 它加载了一个视图。

        所以,传递给$excelreader->load() 的参数可能只是字符串“excel/”。确定这不是一个有效的 zip 对象。

        此序列应为excelreader->load() 生成正确的字符串。

        $this->form();
        $loadexcel = $excelreader->load('excel/'.$this->filename);
        

        但您必须接受视图也会被加载。

        【讨论】:

        • 感谢@DFriend 的回答。我尝试在表单函数中的$this->filename = $upload_data['file_name']; 之后使用return $this->filename; 返回值,但结果是显示空白页。我把返回语法错了吗?我已经尝试$this->load(),但调用未定义方法 Roadmap::load() 仍然出错
        • 我的错误。它应该是 $this->form() 而不是 $this->load()。我已经更正了答案代码。我的回答的真正要点是您必须向$excelreader->load() 提供文件名,直到文件上传后才能这样做。
        猜你喜欢
        • 2020-10-28
        • 1970-01-01
        • 2013-12-16
        • 2014-12-25
        • 1970-01-01
        • 1970-01-01
        • 2012-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多