Why am I getting an "invalid or unified Zip object" error when trying to read an excel file through PHP?

I would like to learn how to read excel file using PHP. In my particular case of using PHPExcel from Yii.

I followed numerous tutorials and I was always stuck in one place: "ZipArchive :: getFromName (): Invalid or unified Zip object." I added extensions, bootloader, etc., but nothing works. is there any way around this? or do i need to get another library? Here is the code in my controller.

Yii::import('application.vendors.PHPExcel.PHPExcel',true); $objReader = PHPExcel_IOFactory::createReader('Excel2007'); $objPHPExcel = $objReader->load('c:\cctv.xls'); //$file --> your filepath and filename $objWorksheet = $objPHPExcel->getActiveSheet(); $highestRow = $objWorksheet->getHighestRow(); // eg 10 $highestColumn = $objWorksheet->getHighestColumn(); // eg 'F' $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // eg 5 echo '<table>' . "\n"; for ($row = 2; $row <= $highestRow; ++$row) { echo '<tr>' . "\n"; for ($col = 0; $col <= $highestColumnIndex; ++$col) { echo '<td>' . $objWorksheet->getCellByColumnAndRow($col, $row)->getValue() . '</td>' . "\n"; } echo '</tr>' . "\n"; } echo '</table>' . "\n"; 

this is a detailed error:

C: \ WAMP \ WWW \ for example \\ protected providers \ PHPExcel \ PHPExcel \ read \ Excel2007.php (272)

 } public function _getFromZipArchive(ZipArchive $archive, $fileName = '') { // Root-relative paths if (strpos($fileName, '//') !== false) { $fileName = substr($fileName, strpos($fileName, '//') + 1); } $fileName = PHPExcel_Shared_File::realpath($fileName); // Apache POI fixes $contents = $archive->getFromName($fileName); if ($contents === false) { $contents = $archive->getFromName(substr($fileName, 1)); } /* if (strpos($contents, '<?xml') !== false && strpos($contents, '<?xml') !== 0) { $contents = substr($contents, strpos($contents, '<?xml')); } var_dump($fileName); var_dump($contents); 

Stack Trace C: \ wamp \ www \ trunk \ protected \ vendors \ PHPExcel \ PHPExcel \ Reader \ Excel2007.php (272): ZipArchive-> getFromName ("_ rels / .rels")

 $fileName = substr($fileName, strpos($fileName, '//') + 1); } $fileName = PHPExcel_Shared_File::realpath($fileName); // Apache POI fixes $contents = $archive->getFromName($fileName); if ($contents === false) { $contents = $archive->getFromName(substr($fileName, 1)); } 

C: \ wamp \ www \ example \ protected \ vendors \ PHPExcel \ PHPExcel \ Reader \ Excel2007.php (312): PHPExcel_Reader_Excel2007 → _ getFromZipArchive (ZipArchive, "_rels / .rels")

 $excel->removeCellXfByIndex(0); // remove the default style } $zip = new ZipArchive; $zip->open($pFilename); $rels = simplexml_load_string($this->_getFromZipArchive($zip, "_rels/.rels")); //~http://schemas.openxmlformats.org/package/2006/relationships"); foreach ($rels->Relationship as $rel) { switch ($rel["Type"]) { case "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties": $xmlCore = simplexml_load_string($this->_getFromZipArchive($zip, "{$rel['Target']}")); if (is_object($xmlCore)) { 

C: \ wamp \ www \ example \ protected \ controllers \ AdminController.php (58): PHPExcel_Reader_Excel2007-> load ("c: \ cctv.xls")

 public function actionCreateSource() { Yii::import('application.vendors.PHPExcel.PHPExcel',true); $objReader = PHPExcel_IOFactory::createReader('Excel2007'); $objPHPExcel = $objReader->load('c:\cctv.xls'); //$file --> your filepath and filename $objWorksheet = $objPHPExcel->getActiveSheet(); $highestRow = $objWorksheet->getHighestRow(); // eg 10 $highestColumn = $objWorksheet->getHighestColumn(); // eg 'F' $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // eg 5 echo '<table>' . "\n"; 
+7
source share
2 answers

It looks like you are installing PHPExcel to use the 2007 format explicitly, but you are trying to open the XLS file. Although I'm not 100% sure, I'm going to guess that the zip code error is that it is trying to unzip the XLS file, and this will fail because it is not archived.

The php zip extension is supposed to work because the error is related to the extension — an invalid or unified Zip object. I assume that you are receiving an invalid Zip object since you are not dealing with a zip file.

If you are trying to open an XLS file, you probably want to:

 $objReader = PHPExcel_IOFactory::createReader('Excel5'); 

Alternatively, you can remove the explicit mode and simply rely on automatic file type resolution, for example:

 $objPHPExcel = PHPExcel_IOFactory::load("c:\cctv.xls"); // Remove the createReader line before this 
+30
source

I had the same error message, but it turned out to be a file permissions problem (as suggested here: PHPExcel Warning: ZipArchive :: getFromName (): Invalid or unified Zip's object ).

Fast chmod 644 in Excel file fixed.

+4
source

All Articles