1--TEST-- 2Test filesize() function: usage variations - file mode & holes in file 3--SKIPIF-- 4<?php 5if (substr(PHP_OS, 0, 3) != 'WIN') { 6 die('skip only valid for Windows'); 7} 8--FILE-- 9<?php 10$file_path = __DIR__; 11require($file_path."/file.inc"); 12 13echo "*** Testing filesize(): usage variations ***\n"; 14echo "\n*** Testing filesize() with data written using different file modes and by creating holes in file ***\n"; 15 16$filename = $file_path."/filesize_variation4.tmp"; 17$string = "Test 2 test the filesize() fn, with data containing all the types like !@@##$%^&*():<>?|~+!;',.\][{}(special) chars, 12345(numeric) chars, and \n(newline char), \t(tab), \0, \r and so on........\0"; 18echo "-- opening the file in 'w' mode and get the size --\n"; 19$file_handle = fopen($filename, "w"); 20var_dump( strlen($string) ); //strlen of the string 21fwrite($file_handle, $string); 22fclose($file_handle); 23var_dump( filesize($filename) ); //size of the file = strlen of string 24clearstatcache(); 25 26echo "-- opening the file in 'wt' mode and get the size --\n"; 27$file_handle = fopen($filename, "wt"); 28var_dump( strlen($string) ); //strlen of the string = 191 bytes 29fwrite($file_handle, $string); 30fclose($file_handle); 31var_dump( filesize($filename) ); //size of the file = 192 bytes != strlen of string 32clearstatcache(); 33 34echo "-- opening the file in 'a' mode, adding data and checking the file --\n"; 35$file_handle = fopen($filename, "a"); 36fwrite($file_handle, "Hello, world"); 37fclose($file_handle); 38var_dump( filesize($filename) ); //204 bytes 39clearstatcache(); 40 41echo "-- opening the file in 'at' mode, adding data and checking the file --\n"; 42$file_handle = fopen($filename, "at"); 43fwrite($file_handle, "Hello, world\n"); 44fclose($file_handle); 45var_dump( filesize($filename) ); //218 bytes 46clearstatcache(); 47 48echo "-- creating a hole and checking the size --\n"; 49$file_handle = fopen($filename, "a"); 50var_dump( ftruncate($file_handle, 220) ); //creating 4 bytes of hole 51fclose($file_handle); 52var_dump( filesize($filename) ); //220 bytes 53clearstatcache(); 54 55echo "-- writing data after hole and checking the size --\n"; 56$file_handle = fopen($filename, "a"); 57fwrite($file_handle, "Hello\0"); //writing 6 bytes of data 58fclose($file_handle); 59var_dump( filesize($filename) ); //226 bytes 60clearstatcache(); 61 62echo "-- opening the existing file in write mode --\n"; 63fclose( fopen($filename, "w") ); 64var_dump( filesize($filename) ); //0 bytes 65clearstatcache(); 66 67echo "-- with empty file --\n"; 68$filename = __DIR__."/filesize_variation4_empty.tmp"; 69fclose( fopen($filename, "w") ); 70var_dump( filesize($filename) ); //0 bytes 71 72echo "*** Done ***\n"; 73?> 74--CLEAN-- 75<?php 76$file_path = __DIR__; 77unlink($file_path."/filesize_variation4.tmp"); 78unlink($file_path."/filesize_variation4_empty.tmp"); 79?> 80--EXPECT-- 81*** Testing filesize(): usage variations *** 82 83*** Testing filesize() with data written using different file modes and by creating holes in file *** 84-- opening the file in 'w' mode and get the size -- 85int(191) 86int(191) 87-- opening the file in 'wt' mode and get the size -- 88int(191) 89int(192) 90-- opening the file in 'a' mode, adding data and checking the file -- 91int(204) 92-- opening the file in 'at' mode, adding data and checking the file -- 93int(218) 94-- creating a hole and checking the size -- 95bool(true) 96int(220) 97-- writing data after hole and checking the size -- 98int(226) 99-- opening the existing file in write mode -- 100int(0) 101-- with empty file -- 102int(0) 103*** Done *** 104