fgetss_variation5.phpt 4.7 KB

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