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