Why PHPExcelReader Fails on XLSX Files
You are encountering a fundamental mismatch between the library you are using and the file format you are trying to read. The core issue lies in the class name: PhpExcelReader. As indicated by the code snippet from coursesweb.net, this specific implementation relies on the php-excel-reader project. While that project is often touted as supporting both .xls and .xlsx, its primary design focusâand historical strengthâhas been parsing legacy binary Excel 97â2003 (.xls) files.
When you attempt to load an .xlsx file with this specific reader, the library fails because it attempts to parse the binary OLE structure of .xls files rather than the Open XML structure used in .xlsx. The error message "file is not readable" is a direct consequence of trying to interpret a ZIP archive as a binary stream. You cannot simply add a configuration flag or tweak the output encoding to fix this architectural incompatibility; the underlying parser logic does not exist for the modern format.
To resolve this, you must switch to a library that explicitly supports Open XML Spreadsheet formats. The standard approach in the PHP ecosystem is to use PhpSpreadsheet, which is now the successor to the original PEAR-based PHPExcel and PHPRcaster libraries. Unlike the lightweight php-excel-reader, PhpSpreadsheet includes full readers for both .xls and .xlsx.
Here is how you should refactor your code to handle .xlsx files correctly:
require_once 'vendor/autoload.php'; // Ensure composer dependencies are loaded
$filename = "uploads/{$filename}";
$fileExtension = pathinfo($filename, PATHINFO_EXTENSION);
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
// Determine the reader based on file extension
if ($fileExtension === 'xlsx') {
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
} elseif ($fileExtension === 'xls') {
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
} else {
throw new InvalidArgumentException("Unsupported file format");
}
// Set output encoding is not strictly needed for reading,
// but useful if you plan to manipulate strings heavily later.
$reader->setReadDataOnly(true); // Optional: Improves performance
$reader->load($filename, $spreadsheet);
// Access the data
$sheetName = $spreadsheet->getActiveSheet()->getTitle();
$maxRows = $spreadsheet->getActiveSheet()->getHighestRow();
$maxCols = $spreadsheet->getActiveSheet()->getHighestColumn();
$data = [];
for ($row = 1; $row <= $maxRows; $row++) {
$rowData = [];
for ($col = 'A'; $col <= $maxCols; $col++) {
// PhpSpreadsheet handles cell coordinates internally,
// but iterating by string column index is common in custom logic.
// However, using getRow() is usually more efficient:
$cell = $spreadsheet->getActiveSheet()->getCell("$col$row");
if ($cell && !$cell->getNullValue()) {
$rowData[$col] = $cell->getValue();
}
}
$data[] = $rowData;
}
The transition requires changing your dependency management. If you are using Composer, the command composer require phpoffice/phpspreadsheet will give you a robust solution that handles version-specific behaviors correctly. Older libraries like the one in your example often lack type safety and modern error handling found in the PhpSpreadsheet package.
One potential pitfall when migrating is memory usage. The PhpExcelReader (old) was designed to be lightweight, whereas PhpSpreadsheet loads the entire workbook into memory by default. For very large datasets, you should consider using PhpSpreadsheet\IOFactory combined with specific reader settings or switching to a stream-based approach if available in your version.
If you require a solution that integrates deeply with a Laravel application, leveraging packages like phpoffice/phpspreadsheet ensures compatibility with modern PHP versions and avoids the deprecated methods found in older PEAR projects. For more comprehensive documentation on handling Excel files within the Laravel ecosystem, you can refer to resources provided by https://laravelcompany.com.