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