xref: /PHP-8.2/ext/standard/tests/file/004.phpt (revision f8d79582)
1--TEST--
2file_put_contents() test
3--FILE--
4<?php
5    chdir(__DIR__);
6    for ($i = 1; $i < 6; $i++) {
7        @unlink("./TEST{$i}");
8    }
9
10    echo "String Test: ";
11    echo file_put_contents("TEST1", file_get_contents(__FILE__)) !== FALSE ? 'OK' : 'FAIL';
12    echo "\n";
13
14    $old_int = $int = rand();
15    $ret = file_put_contents("TEST2", $int);
16    echo "Integer Test: ";
17    if ($int === $old_int && $ret !== FALSE && md5($int) == md5_file("TEST2")) {
18        echo 'OK';
19    } else {
20        echo 'FAIL';
21    }
22    echo "\n";
23
24    $old_int = $int = time() / 1000;
25    $ret = file_put_contents("TEST3", $int);
26    echo "Float Test: ";
27    if ($int === $old_int && $ret !== FALSE && md5($int) == md5_file("TEST3")) {
28        echo 'OK';
29    } else {
30        echo 'FAIL';
31    }
32    echo "\n";
33
34    $ret = file_put_contents("TEST4", __FILE__);
35    echo "Bool Test: ";
36    if ($ret !== FALSE && md5(__FILE__) == md5_file("TEST4")) {
37        echo 'OK';
38    } else {
39        echo 'FAIL';
40    }
41    echo "\n";
42
43    $ret = @file_put_contents("TEST5", $_SERVER);
44    echo "Array Test: ";
45    if ($ret !== FALSE && @md5(implode('', $_SERVER)) == md5_file("TEST5")) {
46        echo 'OK';
47    } else {
48        echo 'FAIL';
49    }
50    echo "\n";
51
52    for ($i = 1; $i < 6; $i++) {
53        @unlink("./TEST{$i}");
54    }
55?>
56--EXPECT--
57String Test: OK
58Integer Test: OK
59Float Test: OK
60Bool Test: OK
61Array Test: OK
62