codeigniter phpexcel error ZipArchive::getFromName(): Invalid or uninitialized Zip object

6.7k Views Asked by At

i'm trying to import data to oracle from excel file (.xlsx) use codeigniter and phpexcel, this is my controller :

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"); 
}

and this is my model :

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);
}

and when i use import function, this is error message :

Message: ZipArchive::getFromName(): Invalid or uninitialized Zip object

Filename: Reader/Excel2007.php

Line Number: 327

Backtrace :

File: C:\xampp\htdocs\web_excel_ci\application\controllers\Roadmap.php

Line: 82

Function: load

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

that is for load which excel file would be to import, i try to get filename from function form() with $this->filename = $this->form(), but that is make an error

please help for solution couse i've stack there

Thanks a lot...

3

There are 3 best solutions below

0
Fajar Ardiansyah On BEST ANSWER

i have got a solution for fix this case after search many-many hours in this stack overflow,

i use the $this->session->set_flashdata('fileName',$fileName); for get the value in one function

and use the $fileName = $this->session->flashdata('fileName'); for put that value in another function

and it's worked.

Thanks for all your attention...

2
DFriend On

There is definitely a problem with the line

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

It appears to be trying to set $this->filename with the return from $this->form(). But $this->form() does not return a value - it loads a view.

So, the argument passed to $excelreader->load() is probably just the string "excel/". For sure that's not a valid zip object.

This sequence should produce the correct string for excelreader->load().

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

But you have to accept that the view will be loaded too.

0
Irfan Ahmed On

From the Docs

By default, PHPWord uses Zip extension to deal with ZIP compressed archives and files inside them. If you can’t have Zip extension installed on your server, you can use pure PHP library alternative, PclZip, which is included in PHPWord.

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

This worked for me.