PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception

Unknown codepage: $codePage

Error message

Unknown codepage: $codePage

What it means

CodePage::numberToName() throws 'Unknown codepage' when the ID from the workbook's CODEPAGE record is not in the reader's map at all. That indicates a value outside every BIFF code page PhpSpreadsheet knows — in practice corrupt data, or a file produced by a broken third-party writer rather than Excel.

Source

Thrown at src/PhpSpreadsheet/Shared/CodePage.php:107

            if (is_array($value)) {
                foreach ($value as $encoding) {
                    if (@iconv('UTF-8', $encoding, ' ') !== false) {
                        self::$pageArray[$codePage] = $encoding;

                        return $encoding;
                    }
                }

                throw new PhpSpreadsheetException("Code page $codePage not implemented on this system.");
            } else {
                return $value;
            }
        }
        if ($codePage == 720 || $codePage == 32769) {
            throw new PhpSpreadsheetException("Code page $codePage not supported."); //    OEM Arabic
        }

        throw new PhpSpreadsheetException('Unknown codepage: ' . $codePage);
    }

    /** @return array<int, array<int, string>|string> */
    public static function getEncodings(): array
    {
        return self::$pageArray;
    }
}

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Verify the file is really an OLE2/BIFF container (leading bytes D0 CF 11 E0 A1 B1 1A E1) or let IOFactory::identify() pick the reader
  2. Re-export or re-download the source file
  3. Repair the file in Excel/LibreOffice first (open + save), then read it
  4. Validate uploads server-side (size, signature) before handing them to the reader
Defensive patterns

Strategy: validation

Validate before calling

function looksLikeOle2Spreadsheet(string $path): bool
{
    $fh = fopen($path, 'rb');
    $sig = fread($fh, 8);
    fclose($fh);

    return $sig === "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1";
}

if (!looksLikeOle2Spreadsheet($path)) {
    throw new InvalidArgumentException('Corrupt or non-Xls upload');
}

Try / catch

try {
    $spreadsheet = $reader->load($path);
} catch (\PhpOffice\PhpSpreadsheet\Reader\Exception $e) {
    // 'Unknown codepage' means corrupt/binary-mangled input: quarantine
    quarantine($path, $e->getMessage());
}

Prevention

When it happens

Trigger: A corrupted .xls where the CODEPAGE record bytes are garbage; binary malformation from non-Excel generators; forcing the Xls reader onto a file that is not BIFF at all.

Common situations: Uploads truncated mid-transfer; files renamed to .xls; aggressive pre-processing or transfer through systems that mangle binary headers (FTP ASCII mode).

Related errors


AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17). Data as JSON: /api/errors/670de1317080289f. Report an issue: GitHub.