PHPExcel XLS not recognised as an OLE file

user3321425 picture user3321425 · Sep 16, 2015 · Viewed 11.8k times · Source

I developed a PHP web application using PHPExcel libraries. All is working properly except with some Excel documents which strangely cannot be recognized as an Excel doc. When I open these files with Microsoft Excel and save them as xls, csv, xlsx, then everything works well. So... I think some Excel files are not exactly what their extension says. Perhaps they are disguised XML? Is it possible to get XML support with PHPExcel?

Here's the error:

Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'The filename /tmp/phphrnIR2 is not recognised as an OLE file' in /home/www/text/excel/reader/Classes/PHPExcel/Shared/OLERead.php:89 Stack trace: #0 /home/test/excel/reader/Classes/PHPExcel/Reader/Excel5.php(1164): PHPExcel_Shared_OLERead->read('/tmp/phphrnIR2') #1 /home/www/text/excel/reader/Classes/PHPExcel/Reader/Excel5.php(612): PHPExcel_Reader_Excel5->_loadOLE('/tmp/phphrnIR2') #2 /home/www/test/excel/actions.php(60): PHPExcel_Reader_Excel5->load('/tmp/phphrnIR2') #3 /home/www/test/excel/index.php(77): include_once('/home/www/test/ex...') #4 {main} thrown in /home/www/test/excel/reader/Classes/PHPExcel/Shared/OLERead.php on line 89

And the php code for file recognition:

 $name     = $_FILES['file']['name'];
                    $tname    = $_FILES['file']['tmp_name'];
                    $type     = $_FILES['file']['type'];

                    if($type == 'application/vnd.ms-excel')
                    {
                            // Excel 97 extension
                            $ext = 'xls';
                    }
                    else if($type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
                    {
                            // Excel 2007 and 2010 extension
                            $ext = 'xlsx';
                    }


                    else if($type == 'text/csv')
                    {
                            // Excel CSV extension
                            $ext = 'csv';


                    }else{

                            // Invalid Extension   
                    ?> 
                            <script languaje="javascript">
                                  swal("Bad file...", "A valid extension must be XLS, XLSX o CSV!", "error");
                            </script> 
                            <?php
                            exit();
                    }
 //reader creation
                    $objReader = PHPExcel_IOFactory::createReader($$ext);

                    //uploading file
                    $objPHPExcel = $objReader->load($tname);


                    $dim = $objPHPExcel->getActiveSheet()->calculateWorksheetDimension();

Answer

Mark Baker picture Mark Baker · Sep 16, 2015

Well, despite the fact that it has a .xls extension, it's entirely possible that it isn't a BIFF-format xls file.

A lot of people give a file an xls extension when it's a csv format file, or even HTML markup..... never trust the extension; and as the mime type will be based on the file extension, you can't take that at face value either.

Rather than specifically creating a Reader based on the extension/mime type.... use the PHPExcel IOFactory identify() method to see what file format PHPExcel believes the file to be. Or call the IOFactory's load() method so that it can try to identify the file format for itself.

Clearly this file isn't BIFF-format (or was created using a very early version of the BIFF-format, before version 5), otherwise it would be recognised as an OLE file