PHPExcel将.xlsx文件标识为CSV文件

我的PHP版本是5.2.9。 我使用PHPExcel 1.7.5来parsingExcel2007的文件。 但是,使用相同的源代码和相同的Excel文件,在两台机器上产生了不同的结果。 这两台机器使用相同的PHP和PHPExcel部署。 一个很好,另一个parsing所有Excel2007文件为CSV文件。

我debugging了这两个案例,最终发现了以下差异。

 public function canRead($pFilename) { // Check if zip class exists if (!class_exists('ZipArchive')) { return false; } // Check if file exists if (!file_exists($pFilename)) { throw new Exception("Could not open " . $pFilename . " for reading! File does not exist."); } $xl = false; // Load file $zip = new ZipArchive; if ($zip->open($pFilename) === true) { // check if it is an OOXML archive $rels = simplexml_load_string($this->_getFromZipArchive($zip, "_rels/.rels")); foreach ($rels->Relationship as $rel) { switch ($rel["Type"]) { case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument": if (basename($rel["Target"]) == 'workbook.xml') { $xl = true; } break; } } $zip->close(); } return $xl; } ... 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)); } return $contents; } 

在work-well机器上打印的$contents值是xml definition tag 。 但另一台机器返回一个emptystring。

$fileName值是"_rels/.rels" 。 我谷歌,发现它是一个关系文件。 压缩在Excel2007文件中了吗? 有人能告诉我关于这个文件的更多信息吗? 以及如何解决这个parsing问题?

仅供参考,我使用了几个代码来testing这个案例,每次都出现这个问题。 代码如下。

 <? require_once("PHPExcel/PHPExcel/IOFactory.php"); echo @PHPExcel_IOFactory::identify('/tmp/styleA.xlsx'); echo "\n"; exit(); ?> 

对不起,我的英语不好。

最后我发现了真正的问题。 它发生在php installation

错误的机器使用php-cgi启动服务,另一个使用php 。 而错误的机器没有添加--enalble-fastcgiconfiguration,虽然它有zip.so扩展名。 如果你想在项目中使用zip ,你最好在安装php时添加--enable-zip (不是--with-zip !!!)configuration。 安装PHP后,添加PHP扩展zip.so不知何故,它可能会出错。