1--TEST--
2Test file_put_contents() and file_get_contents() functions with 5GB string
3--SKIPIF--
4<?php
5if (PHP_INT_SIZE < 5) {
6    // 4=4gb, 5=549gb, 8=9exabytes
7    die("skip PHP_INT_SIZE<5 will not fit test string in RAM");
8}
9if (getenv('SKIP_SLOW_TESTS')) {
10    die('skip slow test');
11}
12function 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}
30if (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 . "file_get_contents_file_put_contents_5gb.bin";
34$tmpfileh = fopen($tmpfile, "wb");
35if ($tmpfileh === false) {
36    die('skip Reason: Unable to create temporary file');
37}
38fclose($tmpfileh);
39unlink($tmpfile);
40if (disk_free_space(dirname($tmpfile)) < 10 * 1024 * 1024 * 1024) {
41    die('skip Reason: Insufficient disk space (less than 10GB)');
42}
43?>
44--INI--
45memory_limit=6G
46--FILE--
47<?php
48$tmpfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "file_get_contents_file_put_contents_5gb.bin";
49$large_string_len = 5 * 1024 * 1024 * 1024;
50
51$large_string = str_repeat('a', $large_string_len);
52$result = file_put_contents($tmpfile, $large_string);
53if ($result !== $large_string_len) {
54    echo "Could only write $result bytes of $large_string_len bytes.";
55    var_dump(error_get_last());
56} else {
57    echo "File written successfully." . PHP_EOL;
58}
59unset($large_string);
60
61$result_large_string = file_get_contents($tmpfile);
62if (strlen($result_large_string) !== $large_string_len) {
63    echo "Could only read " . strlen($result_large_string) . " bytes of $large_string_len bytes.";
64    var_dump(error_get_last());
65} else {
66    echo "File read successfully." . PHP_EOL;
67}
68
69clearstatcache(true, $tmpfile);
70if (file_exists($tmpfile)) {
71    unlink($tmpfile);
72}
73?>
74--CLEAN--
75<?php
76@unlink(sys_get_temp_dir() . DIRECTORY_SEPARATOR . "file_get_contents_file_put_contents_5gb.bin");
77?>
78--EXPECT--
79File written successfully.
80File read successfully.
81