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