|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
 PatchesPull Requests
Pull requests: HistoryAllCommentsChangesGit/SVN commits              [2021-11-26 05:41 UTC] requinix@php.net
 
-Status: Open
+Status: Analyzed
  [2021-11-26 05:41 UTC] requinix@php.net
  [2021-11-26 12:14 UTC] cmb@php.net
 
-Assigned To:
+Assigned To: cmb
  [2021-11-26 12:46 UTC] cmb@php.net
 
-Summary:     stream_get_contents() loading entire file
              into memory
+Summary:     stream_get_contents() may unnecessarily
              overallocate
-PHP Version: 8.0.13
+PHP Version: 7.4
  [2021-11-26 12:46 UTC] cmb@php.net
  [2021-11-29 13:51 UTC] git@php.net
  [2021-11-29 13:51 UTC] git@php.net
 
-Status: Analyzed
+Status: Closed
 | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 01:00:01 2025 UTC | 
Description: ------------ Passing length of -1 to stream_get_contents() with the file position indicator of resource near the end of file still loads the entire file into memory. Passing a fixed length to stream_get_contents() does not produce the same behaviour. Note: when running test script set memory_limit=64M Test script: --------------- <?php $resource = fopen('/tmp/file1', 'a+'); for ($i = 0; $i < 64; ++$i) { $data = random_bytes(1024 * 1024); $position = ftell($resource); fwrite($resource, $data); fseek($resource, $position); stream_get_contents($resource, strlen($data)); } $resource = fopen('/tmp/file2', 'a+'); for ($i = 0; $i < 64; ++$i) { $data = random_bytes(1024 * 1024); $position = ftell($resource); fwrite($resource, $data); fseek($resource, $position); stream_get_contents($resource, -1); } Actual result: -------------- PHP Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 62922784 bytes) in script.php on line 22