fgets_basic.phpt 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. --TEST--
  2. Test fgets() function : basic functionality
  3. --FILE--
  4. <?php
  5. // include the file.inc for common test functions
  6. include ("file.inc");
  7. $file_modes = array("r", "rb", "rt", "r+", "r+b", "r+t");
  8. $file_content_types = array("numeric", "text", "text_with_new_line", "alphanumeric");
  9. echo "*** Testing fgets() : basic functionality ***\n";
  10. foreach($file_modes as $file_mode) {
  11. echo "\n-- Testing fgets() with file opened using mode $file_mode --\n";
  12. foreach($file_content_types as $file_content_type) {
  13. echo "-- File content type : $file_content_type --\n";
  14. /* create files with $file_content_type */
  15. create_files ( __DIR__, 1, $file_content_type, 0755, 50, "w", "fgets_basic", 1, "bytes"); //create a file
  16. $filename = __DIR__."/fgets_basic1.tmp"; // this is name of the file created by create_files()
  17. $file_handle = fopen($filename, $file_mode);
  18. if ( !$file_handle ) {
  19. echo "Error: failed to open file $filename!";
  20. exit();
  21. }
  22. echo "-- fgets() with default length, file pointer at 0 --\n";
  23. var_dump( fgets($file_handle) ); // with default length
  24. var_dump( ftell($file_handle) ); // ensure the file pointer position
  25. var_dump( feof($file_handle) ); // ensure if eof set
  26. echo "-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --\n";
  27. var_dump( rewind($file_handle) );
  28. var_dump( fgets($file_handle, 23) ); // expected: 22 chars
  29. var_dump( ftell($file_handle) ); // ensure the file pointer position
  30. var_dump( feof($file_handle) ); // ensure if eof set
  31. //close file
  32. fclose($file_handle);
  33. // delete file
  34. delete_file($filename);
  35. } // file_content_type loop
  36. } // file_mode loop
  37. echo "Done\n";
  38. ?>
  39. --EXPECT--
  40. *** Testing fgets() : basic functionality ***
  41. -- Testing fgets() with file opened using mode r --
  42. -- File content type : numeric --
  43. -- fgets() with default length, file pointer at 0 --
  44. string(50) "22222222222222222222222222222222222222222222222222"
  45. int(50)
  46. bool(true)
  47. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  48. bool(true)
  49. string(22) "2222222222222222222222"
  50. int(22)
  51. bool(false)
  52. -- File content type : text --
  53. -- fgets() with default length, file pointer at 0 --
  54. string(50) "text text text text text text text text text text "
  55. int(50)
  56. bool(true)
  57. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  58. bool(true)
  59. string(22) "text text text text te"
  60. int(22)
  61. bool(false)
  62. -- File content type : text_with_new_line --
  63. -- fgets() with default length, file pointer at 0 --
  64. string(5) "line
  65. "
  66. int(5)
  67. bool(false)
  68. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  69. bool(true)
  70. string(5) "line
  71. "
  72. int(5)
  73. bool(false)
  74. -- File content type : alphanumeric --
  75. -- fgets() with default length, file pointer at 0 --
  76. string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
  77. int(50)
  78. bool(true)
  79. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  80. bool(true)
  81. string(22) "ab12 ab12 ab12 ab12 ab"
  82. int(22)
  83. bool(false)
  84. -- Testing fgets() with file opened using mode rb --
  85. -- File content type : numeric --
  86. -- fgets() with default length, file pointer at 0 --
  87. string(50) "22222222222222222222222222222222222222222222222222"
  88. int(50)
  89. bool(true)
  90. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  91. bool(true)
  92. string(22) "2222222222222222222222"
  93. int(22)
  94. bool(false)
  95. -- File content type : text --
  96. -- fgets() with default length, file pointer at 0 --
  97. string(50) "text text text text text text text text text text "
  98. int(50)
  99. bool(true)
  100. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  101. bool(true)
  102. string(22) "text text text text te"
  103. int(22)
  104. bool(false)
  105. -- File content type : text_with_new_line --
  106. -- fgets() with default length, file pointer at 0 --
  107. string(5) "line
  108. "
  109. int(5)
  110. bool(false)
  111. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  112. bool(true)
  113. string(5) "line
  114. "
  115. int(5)
  116. bool(false)
  117. -- File content type : alphanumeric --
  118. -- fgets() with default length, file pointer at 0 --
  119. string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
  120. int(50)
  121. bool(true)
  122. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  123. bool(true)
  124. string(22) "ab12 ab12 ab12 ab12 ab"
  125. int(22)
  126. bool(false)
  127. -- Testing fgets() with file opened using mode rt --
  128. -- File content type : numeric --
  129. -- fgets() with default length, file pointer at 0 --
  130. string(50) "22222222222222222222222222222222222222222222222222"
  131. int(50)
  132. bool(true)
  133. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  134. bool(true)
  135. string(22) "2222222222222222222222"
  136. int(22)
  137. bool(false)
  138. -- File content type : text --
  139. -- fgets() with default length, file pointer at 0 --
  140. string(50) "text text text text text text text text text text "
  141. int(50)
  142. bool(true)
  143. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  144. bool(true)
  145. string(22) "text text text text te"
  146. int(22)
  147. bool(false)
  148. -- File content type : text_with_new_line --
  149. -- fgets() with default length, file pointer at 0 --
  150. string(5) "line
  151. "
  152. int(5)
  153. bool(false)
  154. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  155. bool(true)
  156. string(5) "line
  157. "
  158. int(5)
  159. bool(false)
  160. -- File content type : alphanumeric --
  161. -- fgets() with default length, file pointer at 0 --
  162. string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
  163. int(50)
  164. bool(true)
  165. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  166. bool(true)
  167. string(22) "ab12 ab12 ab12 ab12 ab"
  168. int(22)
  169. bool(false)
  170. -- Testing fgets() with file opened using mode r+ --
  171. -- File content type : numeric --
  172. -- fgets() with default length, file pointer at 0 --
  173. string(50) "22222222222222222222222222222222222222222222222222"
  174. int(50)
  175. bool(true)
  176. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  177. bool(true)
  178. string(22) "2222222222222222222222"
  179. int(22)
  180. bool(false)
  181. -- File content type : text --
  182. -- fgets() with default length, file pointer at 0 --
  183. string(50) "text text text text text text text text text text "
  184. int(50)
  185. bool(true)
  186. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  187. bool(true)
  188. string(22) "text text text text te"
  189. int(22)
  190. bool(false)
  191. -- File content type : text_with_new_line --
  192. -- fgets() with default length, file pointer at 0 --
  193. string(5) "line
  194. "
  195. int(5)
  196. bool(false)
  197. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  198. bool(true)
  199. string(5) "line
  200. "
  201. int(5)
  202. bool(false)
  203. -- File content type : alphanumeric --
  204. -- fgets() with default length, file pointer at 0 --
  205. string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
  206. int(50)
  207. bool(true)
  208. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  209. bool(true)
  210. string(22) "ab12 ab12 ab12 ab12 ab"
  211. int(22)
  212. bool(false)
  213. -- Testing fgets() with file opened using mode r+b --
  214. -- File content type : numeric --
  215. -- fgets() with default length, file pointer at 0 --
  216. string(50) "22222222222222222222222222222222222222222222222222"
  217. int(50)
  218. bool(true)
  219. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  220. bool(true)
  221. string(22) "2222222222222222222222"
  222. int(22)
  223. bool(false)
  224. -- File content type : text --
  225. -- fgets() with default length, file pointer at 0 --
  226. string(50) "text text text text text text text text text text "
  227. int(50)
  228. bool(true)
  229. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  230. bool(true)
  231. string(22) "text text text text te"
  232. int(22)
  233. bool(false)
  234. -- File content type : text_with_new_line --
  235. -- fgets() with default length, file pointer at 0 --
  236. string(5) "line
  237. "
  238. int(5)
  239. bool(false)
  240. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  241. bool(true)
  242. string(5) "line
  243. "
  244. int(5)
  245. bool(false)
  246. -- File content type : alphanumeric --
  247. -- fgets() with default length, file pointer at 0 --
  248. string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
  249. int(50)
  250. bool(true)
  251. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  252. bool(true)
  253. string(22) "ab12 ab12 ab12 ab12 ab"
  254. int(22)
  255. bool(false)
  256. -- Testing fgets() with file opened using mode r+t --
  257. -- File content type : numeric --
  258. -- fgets() with default length, file pointer at 0 --
  259. string(50) "22222222222222222222222222222222222222222222222222"
  260. int(50)
  261. bool(true)
  262. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  263. bool(true)
  264. string(22) "2222222222222222222222"
  265. int(22)
  266. bool(false)
  267. -- File content type : text --
  268. -- fgets() with default length, file pointer at 0 --
  269. string(50) "text text text text text text text text text text "
  270. int(50)
  271. bool(true)
  272. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  273. bool(true)
  274. string(22) "text text text text te"
  275. int(22)
  276. bool(false)
  277. -- File content type : text_with_new_line --
  278. -- fgets() with default length, file pointer at 0 --
  279. string(5) "line
  280. "
  281. int(5)
  282. bool(false)
  283. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  284. bool(true)
  285. string(5) "line
  286. "
  287. int(5)
  288. bool(false)
  289. -- File content type : alphanumeric --
  290. -- fgets() with default length, file pointer at 0 --
  291. string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
  292. int(50)
  293. bool(true)
  294. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  295. bool(true)
  296. string(22) "ab12 ab12 ab12 ab12 ab"
  297. int(22)
  298. bool(false)
  299. Done