Solving undefined ZipArchive and undefined variable contents when using PHP Excel

As you know that PHP Excel was deprecated in 2017 and in 2019 it was officially archived. But, before we go to upgrade it to spreedsheet or another library, sometimes we need to solve some project or old project that used this library.

Well, I just want to tell you my experiences when using this library for my project. Its about error ZipArchive not found.

When I browse to the internet, I found that someone gave the solution by upgrading it to the latest version (before deprecated, 1.8.1), but in my case its also not wokring properly.

So, after I try diferent ways, the error was solved by adding this line:

PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);

before you use the library.

Well, the error about ZipArchive that not found was gone. I think in this case we change the source to use system zip/php zip instead the zip from the library? Correct me if I wrong. Because before that I allready try to install php zip to my server.

Forget it. But why it still not working? Then I found something error like this one:

Message: Undefined variable: contentsFilename: Shared/ZipArchive.php

StackOverflow not answer me, then I try different method to call the library and it working well. What I did?

Before,

$excelreader = new PHPExcel_Reader_Excel2007();
$loadexcel = $excelreader->load('uploads/excel/'.$data_upload['file_name']);

After,

$excelreader = PHPExcel_IOFactory::load('uploads/excel/'.$data_upload['file_name']);

And before that lines do not forget to add

PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);

And taraa….

The complete example you can look at my code, I hope it can help us someday.

include APPPATH.'libraries/PHPExcel.php';
$data_upload = $this->upload->data();
PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);
$excelreader = PHPExcel_IOFactory::load('uploads/excel/'.$data_upload['file_name']);
$sheet = $excelreader->getActiveSheet()->toArray(null, true, true ,true);
$data = array();
$numrow = 1;
foreach($sheet as $row){
if($numrow > 1){
array_push($data, array(
//something to push
));
}
$numrow++;
}