简体   繁体   中英

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

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...

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.

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.

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...

Not directly related to but may interrest those using PHPWord and who are getting the same error message:

The same type of problem arrives with PHPWord when you use ".doc" files instead of ".docx".

Convert all your ".doc" files into ".docx" files, particularly if you want to use TemplateProcessor that supports only ".docx" files .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM