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