moodle/moodle · error · PhpOffice\PhpSpreadsheet\Reader\Exception

Unable to identify a reader for this file

Error message

Unable to identify a reader for this file

What it means

Thrown by IOFactory::createReaderForFile() when no available reader's canRead() method returns true for the given file. After an initial guess from the file extension fails, the method iterates every registered reader (or the tested subset) probing the file's actual signature/content; reaching the end of that loop without a match means the file is not a recognizable spreadsheet format (corrupt download, wrong extension, or a format PhpSpreadsheet cannot parse, e.g. ODS saved as .xlsx or a binary Xlsb).

Solutions

  1. Verify the file opens in Excel/LibreOffice and is a supported format (xlsx, xls, csv, ods, gnumeric, slk, xml)
  2. Use IOFactory::identify($filename) to see what is detected before loading
  3. Re-export/convert the file to xlsx
  4. If a custom format, register a reader that canRead() returns true for it

Example fix

// before
$spreadsheet = IOFactory::load($path);
// after
$type = IOFactory::identify($path);
if ($type === 'Csv') { /* configure CSV reader options */ }
$reader = IOFactory::createReader($type);
$spreadsheet = $reader->load($path);
Defensive patterns

Strategy: try-catch

Validate before calling

$type = IOFactory::identify($filename); if ($type === null || $type === 'Csv' && !expectingCsv()) { /* abort */ }

Try / catch

try { $spreadsheet = IOFactory::load($filename); } catch (\PhpOffice\PhpSpreadsheet\Reader\Exception $e) { // reject upload: unrecognized format
  throw new \RuntimeException('Unsupported spreadsheet file'); }

Prevention

When it happens

Trigger: Calling IOFactory::load()/identify() on a file whose content no registered reader recognizes — corrupt/truncated xlsx, wrong extension vs actual content, an unsupported format (e.g. .numbers, .xlsb), or an empty file.

Common situations: Users renaming files to .xlsx without converting, exporting tools producing malformed files, encrypted/password-protected workbooks, uploading non-spreadsheet files.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/db36d4ada44427ae. Report an issue: GitHub.

Appendix: source

Thrown at public/lib/phpspreadsheet/phpspreadsheet/src/PhpSpreadsheet/IOFactory.php:180

            // Let's see if we are lucky
            if ($reader->canRead($filename)) {
                return $reader;
            }
        }

        // If we reach here then "lucky guess" didn't give any result
        // Try walking through all the options in self::$readers (or the selected subset)
        foreach ($testReaders as $readerType => $class) {
            //    Ignore our original guess, we know that won't work
            if ($readerType !== $guessedReader) {
                $reader = self::createReader($readerType);
                if ($reader->canRead($filename)) {
                    return $reader;
                }
            }
        }

        throw new Reader\Exception('Unable to identify a reader for this file');
    }

    /**
     * Guess a reader type from the file extension, if any.
     */
    private static function getReaderTypeFromExtension(string $filename): ?string
    {
        $pathinfo = pathinfo($filename);
        if (!isset($pathinfo['extension'])) {
            return null;
        }

        return match (strtolower($pathinfo['extension'])) {
            // Excel (OfficeOpenXML) Spreadsheet
            'xlsx',
            // Excel (OfficeOpenXML) Macro Spreadsheet (macros will be discarded)
            'xlsm',
            // Excel (OfficeOpenXML) Template

View on GitHub (pinned to 48ea8c3322)