{"record":{"id":"57939d623ef75451","repo":"PHPOffice/PhpSpreadsheet","slug":"unable-to-open-php-memory","errorCode":null,"errorMessage":"Unable to open php://memory","messagePattern":"Unable to open php://memory","errorType":"exception","errorClass":"ReaderException","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Reader/Csv.php","lineNumber":317,"sourceCode":"        if ($this->inputEncoding === self::GUESS_ENCODING) {\n            $this->inputEncoding = self::guessEncoding($filename, $this->fallbackEncoding);\n        }\n        $this->openFile($filename);\n        if ($this->inputEncoding !== 'UTF-8') {\n            $this->convertNonUtf8($filename);\n        }\n    }\n\n    protected function convertNonUtf8(string $filename): void\n    {\n        fclose($this->fileHandle);\n        $entireFile = file_get_contents($filename);\n        if ($entireFile === false) {\n            throw new ReaderException(\"Unable to get contents of $filename\"); // @codeCoverageIgnore\n        }\n        $fileHandle = fopen('php://memory', 'r+b');\n        if ($fileHandle === false) {\n            throw new ReaderException('Unable to open php://memory'); // @codeCoverageIgnore\n        }\n        $this->fileHandle = $fileHandle;\n        $data = StringHelper::convertEncoding($entireFile, 'UTF-8', $this->inputEncoding);\n        fwrite($this->fileHandle, $data);\n        $this->skipBOM();\n    }\n\n    public function setTestAutoDetect(bool $value): self\n    {\n        $this->testAutodetect = $value;\n\n        return $this;\n    }\n\n    private function setAutoDetect(?string $value, int $version = PHP_VERSION_ID): ?string\n    {\n        $retVal = null;\n        if ($value !== null && $this->testAutodetect && $version < 90000) {","sourceCodeStart":299,"sourceCodeEnd":335,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Reader/Csv.php#L299-L335","documentation":"After re-encoding a non-UTF-8 file to UTF-8, the Csv reader stores the converted bytes in a php://memory stream. fopen('php://memory') failing means the process could not allocate that memory block — in practice, memory_limit is nearly exhausted by a large input file (converted copy roughly the size of the file). Upstream marks it @codeCoverageIgnore because it is essentially unreachable except under memory pressure.","triggerScenarios":"Loading a large non-UTF-8 CSV (tens/hundreds of MB) with a tight memory_limit; memory already consumed by earlier payloads in the same request before the reader runs.","commonSituations":"Batch imports on shared hosting with memory_limit=128M; containers with hard memory caps; legacy latin1/cp1252 exports from ERP systems.","solutions":["Raise memory_limit (ini_set or php.ini -d) to comfortably exceed the file size","Pre-convert the file to UTF-8 externally (iconv -f cp1252 -t utf-8) so convertNonUtf8() is never entered","Split oversized files into chunks and load them incrementally"],"exampleFix":"// before\nini_set('memory_limit', '128M');\n$spreadsheet = (new Csv())->load('huge-latin1.csv'); // memory stream alloc fails\n\n// after\nini_set('memory_limit', '1G');\n$spreadsheet = (new Csv())->load('huge-latin1.csv');\n// or better: iconv -f cp1252 -t utf-8 huge-latin1.csv > huge-utf8.csv, then load that","handlingStrategy":"validation","validationCode":"function bytesFromShorthand(string $v): int { $u = strtolower(substr($v, -1)); $n = (int) $v; return match ($u) { 'g' => $n * 1024 ** 3, 'm' => $n * 1024 ** 2, 'k' => $n * 1024, default => (int) $v }; }\n$headroom = bytesFromShorthand(ini_get('memory_limit')) - memory_get_usage(true);\nif (filesize($file) * 2 > $headroom) {\n    ini_set('memory_limit', ceil(filesize($file) * 3 / 1048576) . 'M');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Budget roughly 2-3× file size of memory for non-UTF-8 CSV loads","Pre-convert encodings with iconv at the CLI so the reader's memory-stream path never runs","Chunk very large imports instead of loading whole files"],"tags":["csv","memory-limit","encoding","environment"],"backgroundTag":"memory-exhausted","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}