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/* 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$file_path = dirname(__FILE__); 17require($file_path."/file.inc"); 18 19echo "*** Testing filesize(): usage variations ***\n"; 20echo "\n*** Testing filesize() with data written using different file modes and by creating holes in file ***\n"; 21 22$filename = $file_path."/filesize_variation4.tmp"; 23$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"; 24echo "-- opening the file in 'w' mode and get the size --\n"; 25$file_handle = fopen($filename, "w"); 26var_dump( strlen($string) ); //strlen of the string 27fwrite($file_handle, $string); 28fclose($file_handle); 29var_dump( filesize($filename) ); //size of the file = strlen of string 30clearstatcache(); 31 32echo "-- opening the file in 'wt' mode and get the size --\n"; 33$file_handle = fopen($filename, "wt"); 34var_dump( strlen($string) ); //strlen of the string = 191 bytes 35fwrite($file_handle, $string); 36fclose($file_handle); 37var_dump( filesize($filename) ); //size of the file = 192 bytes != strlen of string 38clearstatcache(); 39 40echo "-- opening the file in 'a' mode, adding data and checking the file --\n"; 41$file_handle = fopen($filename, "a"); 42fwrite($file_handle, "Hello, world"); 43fclose($file_handle); 44var_dump( filesize($filename) ); //204 bytes 45clearstatcache(); 46 47echo "-- opening the file in 'at' mode, adding data and checking the file --\n"; 48$file_handle = fopen($filename, "at"); 49fwrite($file_handle, "Hello, world\n"); 50fclose($file_handle); 51var_dump( filesize($filename) ); //218 bytes 52clearstatcache(); 53 54echo "-- creating a hole and checking the size --\n"; 55$file_handle = fopen($filename, "a"); 56var_dump( ftruncate($file_handle, 220) ); //creating 4 bytes of hole 57fclose($file_handle); 58var_dump( filesize($filename) ); //220 bytes 59clearstatcache(); 60 61echo "-- writing data after hole and checking the size --\n"; 62$file_handle = fopen($filename, "a"); 63fwrite($file_handle, "Hello\0"); //wrting 6 bytes of data 64fclose($file_handle); 65var_dump( filesize($filename) ); //226 bytes 66clearstatcache(); 67 68echo "-- opening the existing file in write mode --\n"; 69fclose( fopen($filename, "w") ); 70var_dump( filesize($filename) ); //0 bytes 71clearstatcache(); 72 73echo "-- with empty file --\n"; 74$filename = dirname(__FILE__)."/filesize_variation4_empty.tmp"; 75fclose( fopen($filename, "w") ); 76var_dump( filesize($filename) ); //0 bytes 77 78echo "*** Done ***\n"; 79?> 80--CLEAN-- 81<?php 82$file_path = dirname(__FILE__); 83unlink($file_path."/filesize_variation4.tmp"); 84unlink($file_path."/filesize_variation4_empty.tmp"); 85?> 86--EXPECTF-- 87*** Testing filesize(): usage variations *** 88 89*** Testing filesize() with data written using different file modes and by creating holes in file *** 90-- opening the file in 'w' mode and get the size -- 91int(191) 92int(191) 93-- opening the file in 'wt' mode and get the size -- 94int(191) 95int(192) 96-- opening the file in 'a' mode, adding data and checking the file -- 97int(204) 98-- opening the file in 'at' mode, adding data and checking the file -- 99int(218) 100-- creating a hole and checking the size -- 101bool(true) 102int(220) 103-- writing data after hole and checking the size -- 104int(226) 105-- opening the existing file in write mode -- 106int(0) 107-- with empty file -- 108int(0) 109*** Done *** 110