xref: /PHP-8.4/ext/standard/tests/file/ftruncate.phpt (revision ea256a21)
1--TEST--
2ftruncate() tests
3--FILE--
4<?php
5
6$filename = __DIR__."/ftruncate.dat";
7
8file_put_contents($filename, "some test data inside");
9
10$fp = fopen($filename, "r");
11var_dump(ftruncate($fp, 10));
12fclose($fp);
13var_dump(file_get_contents($filename));
14
15$fp = fopen($filename, "w");
16var_dump(ftruncate($fp, 10));
17fclose($fp);
18var_dump(file_get_contents($filename));
19
20file_put_contents($filename, "some test data inside");
21
22$fp = fopen($filename, "a");
23var_dump(ftruncate($fp, 10));
24fclose($fp);
25var_dump(file_get_contents($filename));
26
27$fp = fopen($filename, "a");
28var_dump(ftruncate($fp, 0));
29fclose($fp);
30var_dump(file_get_contents($filename));
31
32file_put_contents($filename, "some test data inside");
33
34$fp = fopen($filename, "a");
35try {
36    var_dump(ftruncate($fp, -1000000000));
37} catch (\ValueError $e) {
38    echo $e->getMessage() . \PHP_EOL;
39}
40fclose($fp);
41var_dump(file_get_contents($filename));
42
43@unlink($filename);
44
45?>
46--EXPECTF--
47bool(false)
48string(21) "some test data inside"
49bool(true)
50string(10) "%0%0%0%0%0%0%0%0%0%0"
51bool(true)
52string(10) "some test "
53bool(true)
54string(0) ""
55ftruncate(): Argument #2 ($size) must be greater than or equal to 0
56string(21) "some test data inside"
57