|
| 1 | +--TEST-- |
| 2 | +Test file_put_contents() function with 5GB string |
| 3 | +--SKIPIF-- |
| 4 | +<?php |
| 5 | +if (PHP_INT_SIZE < 5) { |
| 6 | + // 4=4gb, 5=549gb, 8=9exabytes |
| 7 | + skip("skip PHP_INT_SIZE<5 will not fit test string in RAM"); |
| 8 | +} |
| 9 | +if (getenv('SKIP_SLOW_TESTS')) { |
| 10 | + die('skip slow test'); |
| 11 | +} |
| 12 | +function get_system_memory(): int|float|false |
| 13 | +{ |
| 14 | + if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
| 15 | + // Windows-based memory check |
| 16 | + @exec('wmic OS get FreePhysicalMemory', $output); |
| 17 | + if (isset($output[1])) { |
| 18 | + return ((int)trim($output[1])) * 1024; |
| 19 | + } |
| 20 | + } else { |
| 21 | + // Unix/Linux-based memory check |
| 22 | + $memInfo = @file_get_contents("/proc/meminfo"); |
| 23 | + if ($memInfo) { |
| 24 | + preg_match('/MemFree:\s+(\d+) kB/', $memInfo, $matches); |
| 25 | + return $matches[1] * 1024; // Convert to bytes |
| 26 | + } |
| 27 | + } |
| 28 | + return false; |
| 29 | +} |
| 30 | +if (get_system_memory() < 10 * 1024 * 1024 * 1024) { |
| 31 | + die('skip Reason: Insufficient RAM (less than 10GB)'); |
| 32 | +} |
| 33 | +$tmpfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "test_file_put_contents_5gb.bin"; |
| 34 | +$tmpfileh = fopen($tmpfile, "wb"); |
| 35 | +if ($tmpfileh === false) { |
| 36 | + die('skip Reason: Unable to create temporary file'); |
| 37 | +} |
| 38 | +fclose($tmpfileh); |
| 39 | +unlink($tmpfile); |
| 40 | +if (disk_free_space(dirname($tmpfile)) < 10 * 1024 * 1024 * 1024) { |
| 41 | + die('skip Reason: Insufficient disk space (less than 10GB)'); |
| 42 | +} |
| 43 | +?> |
| 44 | +--INI-- |
| 45 | +memory_limit=6G |
| 46 | +--FILE-- |
| 47 | +<?php |
| 48 | +$tmpfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "test_file_put_contents_5gb.bin"; |
| 49 | +$large_string = str_repeat('a', 5 * 1024 * 1024 * 1024); |
| 50 | +$result = file_put_contents($tmpfile, $large_string); |
| 51 | +if ($result !== strlen($large_string)) { |
| 52 | + echo "Could only write $result bytes of " . strlen($large_string) . " bytes."; |
| 53 | + var_dump(error_get_last()); |
| 54 | +} else { |
| 55 | + echo "File written successfully."; |
| 56 | +} |
| 57 | +clearstatcache(true, $tmpfile); |
| 58 | +if (file_exists($tmpfile)) { |
| 59 | + unlink($tmpfile); |
| 60 | +} |
| 61 | +?> |
| 62 | +--CLEAN-- |
| 63 | +<?php |
| 64 | +@unlink(sys_get_temp_dir() . DIRECTORY_SEPARATOR . "test_file_put_contents_5gb.bin"); |
| 65 | +?> |
| 66 | +--EXPECT-- |
| 67 | +File written successfully. |
0 commit comments