1--TEST-- 2Test filesize() function: usage variations - file size after truncate 3--FILE-- 4<?php 5$file_path = __DIR__; 6 7echo "*** Testing filesize(): usage variations ***\n"; 8$filename = $file_path."/filesize_variation3.tmp"; 9$file_handle = fopen($filename, "w"); 10fwrite($file_handle, str_repeat("Hello,World ", 1000) ); // create file of size 12000 bytes 11fclose($file_handle); 12 13echo "-- Testing filesize() after truncating the file to a new length --\n"; 14// truncate the file created earlier in subdir, the size of the file is 12000bytes 15// truncate the same file, in the loop , each time with the decrement in size by 1200 bytes, 16// until -1200bytes size 17for($size = filesize($filename); $size>=-1200; $size-=1200) { 18 $file_handle = fopen($filename, "r+"); 19 try { 20 var_dump( ftruncate($file_handle, $size) ); 21 } catch (\ValueError $e) { 22 echo $e->getMessage() . \PHP_EOL; 23 } 24 fclose($file_handle); 25 var_dump( filesize($filename) ); 26 clearstatcache(); 27} 28 29?> 30--CLEAN-- 31<?php 32$file_path = __DIR__; 33unlink($file_path."/filesize_variation3.tmp"); 34?> 35--EXPECT-- 36*** Testing filesize(): usage variations *** 37-- Testing filesize() after truncating the file to a new length -- 38bool(true) 39int(12000) 40bool(true) 41int(10800) 42bool(true) 43int(9600) 44bool(true) 45int(8400) 46bool(true) 47int(7200) 48bool(true) 49int(6000) 50bool(true) 51int(4800) 52bool(true) 53int(3600) 54bool(true) 55int(2400) 56bool(true) 57int(1200) 58bool(true) 59int(0) 60ftruncate(): Argument #2 ($size) must be greater than or equal to 0 61int(0) 62