fgetss_variation5.phpt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. --TEST--
  2. Test fgetss() function : usage variations - read/write modes, file pointer at EOF
  3. --SKIPIF--
  4. <?php
  5. if (substr(PHP_OS, 0, 3) == 'WIN') {
  6. die('skip.. Not valid for Windows');
  7. }
  8. ?>
  9. --FILE--
  10. <?php
  11. error_reporting(E_ALL & ~E_DEPRECATED);
  12. /*
  13. Prototype: string fgetss ( resource $handle [, int $length [, string $allowable_tags]] );
  14. Description: Gets line from file pointer and strip HTML tags
  15. */
  16. /* try fgetss on files which are opened in read/write modes
  17. w+, w+b, w+t,
  18. a+, a+b, a+t,
  19. x+, x+b, x+t
  20. */
  21. echo "*** Testing fgetss() : usage variations ***\n";
  22. /* string with html and php tags */
  23. $string_with_tags = <<<EOT
  24. <test>Testing fgetss() functions</test>
  25. <?php echo "this string is within php tag"; ?> {;}<{> this
  26. is a heredoc string. <pg>ksklnm@@$$&$&^%&^%&^%&</pg>
  27. <html> html </html> <?php echo "php"; ?>
  28. this line is without any html and php tags
  29. this is a line with more than eighty character,want to check line splitting correctly after 80 characters
  30. this text contains some html tags <body> body </body> <br> br </br>
  31. this is the line with \n character.
  32. EOT;
  33. $filename = dirname(__FILE__)."/fgetss_variation5.tmp";
  34. /* try reading the file opened in different modes of reading */
  35. $file_modes = array("w+","w+b", "w+t","a+", "a+b", "a+t","x+","x+b","x+t");
  36. for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
  37. echo "\n-- Testing fgetss() with file opened using $file_modes[$mode_counter] mode --\n";
  38. /* create an empty file and write the strings with tags */
  39. $file_handle = fopen($filename, $file_modes[$mode_counter]);
  40. fwrite($file_handle,$string_with_tags); //writing data to the file
  41. if(!$file_handle) {
  42. echo "Error: failed to open file $filename!\n";
  43. exit();
  44. }
  45. // rewind the file pointer to beginning of the file
  46. var_dump( filesize($filename) );
  47. var_dump( rewind($file_handle) );
  48. var_dump( ftell($file_handle) );
  49. var_dump( feof($file_handle) );
  50. echo "-- Reading when file pointer points to EOF --\n";
  51. var_dump( fseek($file_handle,0,SEEK_END) ); // now file pointer at end
  52. var_dump( ftell($file_handle) ); //ensure file pointer at end
  53. var_dump( fgetss($file_handle) ); // try to read
  54. var_dump( ftell($file_handle) ); // find out file position
  55. var_dump( feof($file_handle) ); // ensure that file pointer is at eof
  56. // now file is at the end try reading with length and allowable tags,expecting false
  57. var_dump( fgetss($file_handle, 80, "<test>, <html>, <?>") );
  58. var_dump( ftell($file_handle) ); // find out file position
  59. var_dump( feof($file_handle) ); // ensure that file pointer is at eof
  60. // close the file
  61. fclose($file_handle);
  62. // delete the file
  63. unlink($filename);
  64. } // end of for - mode_counter
  65. echo "Done\n";
  66. ?>
  67. --EXPECT--
  68. *** Testing fgetss() : usage variations ***
  69. -- Testing fgetss() with file opened using w+ mode --
  70. int(445)
  71. bool(true)
  72. int(0)
  73. bool(false)
  74. -- Reading when file pointer points to EOF --
  75. int(0)
  76. int(445)
  77. bool(false)
  78. int(445)
  79. bool(true)
  80. bool(false)
  81. int(445)
  82. bool(true)
  83. -- Testing fgetss() with file opened using w+b mode --
  84. int(445)
  85. bool(true)
  86. int(0)
  87. bool(false)
  88. -- Reading when file pointer points to EOF --
  89. int(0)
  90. int(445)
  91. bool(false)
  92. int(445)
  93. bool(true)
  94. bool(false)
  95. int(445)
  96. bool(true)
  97. -- Testing fgetss() with file opened using w+t mode --
  98. int(445)
  99. bool(true)
  100. int(0)
  101. bool(false)
  102. -- Reading when file pointer points to EOF --
  103. int(0)
  104. int(445)
  105. bool(false)
  106. int(445)
  107. bool(true)
  108. bool(false)
  109. int(445)
  110. bool(true)
  111. -- Testing fgetss() with file opened using a+ mode --
  112. int(445)
  113. bool(true)
  114. int(0)
  115. bool(false)
  116. -- Reading when file pointer points to EOF --
  117. int(0)
  118. int(445)
  119. bool(false)
  120. int(445)
  121. bool(true)
  122. bool(false)
  123. int(445)
  124. bool(true)
  125. -- Testing fgetss() with file opened using a+b mode --
  126. int(445)
  127. bool(true)
  128. int(0)
  129. bool(false)
  130. -- Reading when file pointer points to EOF --
  131. int(0)
  132. int(445)
  133. bool(false)
  134. int(445)
  135. bool(true)
  136. bool(false)
  137. int(445)
  138. bool(true)
  139. -- Testing fgetss() with file opened using a+t mode --
  140. int(445)
  141. bool(true)
  142. int(0)
  143. bool(false)
  144. -- Reading when file pointer points to EOF --
  145. int(0)
  146. int(445)
  147. bool(false)
  148. int(445)
  149. bool(true)
  150. bool(false)
  151. int(445)
  152. bool(true)
  153. -- Testing fgetss() with file opened using x+ mode --
  154. int(445)
  155. bool(true)
  156. int(0)
  157. bool(false)
  158. -- Reading when file pointer points to EOF --
  159. int(0)
  160. int(445)
  161. bool(false)
  162. int(445)
  163. bool(true)
  164. bool(false)
  165. int(445)
  166. bool(true)
  167. -- Testing fgetss() with file opened using x+b mode --
  168. int(445)
  169. bool(true)
  170. int(0)
  171. bool(false)
  172. -- Reading when file pointer points to EOF --
  173. int(0)
  174. int(445)
  175. bool(false)
  176. int(445)
  177. bool(true)
  178. bool(false)
  179. int(445)
  180. bool(true)
  181. -- Testing fgetss() with file opened using x+t mode --
  182. int(445)
  183. bool(true)
  184. int(0)
  185. bool(false)
  186. -- Reading when file pointer points to EOF --
  187. int(0)
  188. int(445)
  189. bool(false)
  190. int(445)
  191. bool(true)
  192. bool(false)
  193. int(445)
  194. bool(true)
  195. Done