{"record":{"id":"db36d4ada44427ae","repo":"moodle/moodle","slug":"unable-to-identify-a-reader-for-this-file","errorCode":null,"errorMessage":"Unable to identify a reader for this file","messagePattern":"Unable to identify a reader for this file","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Reader\\Exception","httpStatus":null,"severity":"error","filePath":"public/lib/phpspreadsheet/phpspreadsheet/src/PhpSpreadsheet/IOFactory.php","lineNumber":180,"sourceCode":"            // Let's see if we are lucky\n            if ($reader->canRead($filename)) {\n                return $reader;\n            }\n        }\n\n        // If we reach here then \"lucky guess\" didn't give any result\n        // Try walking through all the options in self::$readers (or the selected subset)\n        foreach ($testReaders as $readerType => $class) {\n            //    Ignore our original guess, we know that won't work\n            if ($readerType !== $guessedReader) {\n                $reader = self::createReader($readerType);\n                if ($reader->canRead($filename)) {\n                    return $reader;\n                }\n            }\n        }\n\n        throw new Reader\\Exception('Unable to identify a reader for this file');\n    }\n\n    /**\n     * Guess a reader type from the file extension, if any.\n     */\n    private static function getReaderTypeFromExtension(string $filename): ?string\n    {\n        $pathinfo = pathinfo($filename);\n        if (!isset($pathinfo['extension'])) {\n            return null;\n        }\n\n        return match (strtolower($pathinfo['extension'])) {\n            // Excel (OfficeOpenXML) Spreadsheet\n            'xlsx',\n            // Excel (OfficeOpenXML) Macro Spreadsheet (macros will be discarded)\n            'xlsm',\n            // Excel (OfficeOpenXML) Template","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/phpspreadsheet/phpspreadsheet/src/PhpSpreadsheet/IOFactory.php#L162-L198","documentation":"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).","triggerScenarios":"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.","commonSituations":"Users renaming files to .xlsx without converting, exporting tools producing malformed files, encrypted/password-protected workbooks, uploading non-spreadsheet files.","solutions":["Verify the file opens in Excel/LibreOffice and is a supported format (xlsx, xls, csv, ods, gnumeric, slk, xml)","Use IOFactory::identify($filename) to see what is detected before loading","Re-export/convert the file to xlsx","If a custom format, register a reader that canRead() returns true for it"],"exampleFix":"// before\n$spreadsheet = IOFactory::load($path);\n// after\n$type = IOFactory::identify($path);\nif ($type === 'Csv') { /* configure CSV reader options */ }\n$reader = IOFactory::createReader($type);\n$spreadsheet = $reader->load($path);","handlingStrategy":"try-catch","validationCode":"$type = IOFactory::identify($filename); if ($type === null || $type === 'Csv' && !expectingCsv()) { /* abort */ }","typeGuard":null,"tryCatchPattern":"try { $spreadsheet = IOFactory::load($filename); } catch (\\PhpOffice\\PhpSpreadsheet\\Reader\\Exception $e) { // reject upload: unrecognized format\n  throw new \\RuntimeException('Unsupported spreadsheet file'); }","preventionTips":["Call IOFactory::identify() before load to fail fast","Validate uploads by content, not just extension","Keep file size/mime checks and re-export unsupported formats"],"tags":["php","phpspreadsheet","file-format"],"backgroundTag":"file-not-found","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}