fgets_basic.phpt 9.1 KB

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