fgetcsv_variation29.phpt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. --TEST--
  2. Test fgetcsv() : usage variations - with only file handle as argument, file pointer pointing at end of file
  3. --FILE--
  4. <?php
  5. /*
  6. Prototype: array fgetcsv ( resource $handle [, int $length [, string $delimiter [, string $enclosure]]] );
  7. Description: Gets line from file pointer and parse for CSV fields
  8. */
  9. /*
  10. Testing fgetcsv() to read a file whose file pointer is pointing to end of file
  11. and fgetcsv() provided with only file handle in its argument
  12. */
  13. echo "*** Testing fgetcsv() : with file handle as only argument and file pointer pointing at end of file ***\n";
  14. /* the array is with two elements in it. Each element should be read as
  15. 1st element is delimiter & 2nd element is csv fields
  16. */
  17. $csv_lists = array (
  18. array(',', 'water,fruit'),
  19. array(' ', 'water fruit'),
  20. array(' ', '"water" "fruit"'),
  21. array('\\', 'water\\"fruit"\\"air"'),
  22. array('\\', '"water"\\"fruit"\\"""'),
  23. );
  24. $filename = dirname(__FILE__) . '/fgetcsv_variation29.tmp';
  25. @unlink($filename);
  26. $file_modes = array ("r","rb", "rt", "r+", "r+b", "r+t",
  27. "a+", "a+b", "a+t",
  28. "w+", "w+b", "w+t",
  29. "x+", "x+b", "x+t");
  30. $loop_counter = 1;
  31. foreach ($csv_lists as $csv_list) {
  32. for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
  33. // create the file and add the content with has csv fields
  34. if ( strstr($file_modes[$mode_counter], "r") ) {
  35. $file_handle = fopen($filename, "w");
  36. } else {
  37. $file_handle = fopen($filename, $file_modes[$mode_counter] );
  38. }
  39. if ( !$file_handle ) {
  40. echo "Error: failed to create file $filename!\n";
  41. exit();
  42. }
  43. $delimiter = $csv_list[0];
  44. $csv_field = $csv_list[1];
  45. fwrite($file_handle, $csv_field . "\n");
  46. // write another line of text and a blank line
  47. // this will be used to test, if the fgetcsv() read more than a line and its
  48. // working when only a blan line is read
  49. fwrite($file_handle, "This is line of text without csv fields\n");
  50. fwrite($file_handle, "\n"); // blank line
  51. // close the file if the mode to be used is read mode and re-open using read mode
  52. // else rewind the file pointer to beginning of the file
  53. if ( strstr($file_modes[$mode_counter], "r" ) ) {
  54. fclose($file_handle);
  55. $file_handle = fopen($filename, $file_modes[$mode_counter]);
  56. }
  57. echo "\n-- Testing fgetcsv() with file opened using $file_modes[$mode_counter] mode --\n";
  58. // set the file pointer to EOF
  59. var_dump( fseek($file_handle, 0, SEEK_END) );
  60. // call fgetcsv() to parse csv fields
  61. // now file pointer should point to end of the file, try reading again
  62. var_dump( feof($file_handle) );
  63. var_dump( fgetcsv($file_handle) );
  64. // check the file pointer position and if eof
  65. var_dump( ftell($file_handle) );
  66. var_dump( feof($file_handle) );
  67. // close the file
  68. fclose($file_handle);
  69. //delete file
  70. unlink($filename);
  71. } //end of mode loop
  72. } // end of foreach
  73. echo "Done\n";
  74. ?>
  75. --EXPECTF--
  76. *** Testing fgetcsv() : with file handle as only argument and file pointer pointing at end of file ***
  77. -- Testing fgetcsv() with file opened using r mode --
  78. int(0)
  79. bool(false)
  80. bool(false)
  81. int(53)
  82. bool(true)
  83. -- Testing fgetcsv() with file opened using rb mode --
  84. int(0)
  85. bool(false)
  86. bool(false)
  87. int(53)
  88. bool(true)
  89. -- Testing fgetcsv() with file opened using rt mode --
  90. int(0)
  91. bool(false)
  92. bool(false)
  93. int(%d)
  94. bool(true)
  95. -- Testing fgetcsv() with file opened using r+ mode --
  96. int(0)
  97. bool(false)
  98. bool(false)
  99. int(53)
  100. bool(true)
  101. -- Testing fgetcsv() with file opened using r+b mode --
  102. int(0)
  103. bool(false)
  104. bool(false)
  105. int(53)
  106. bool(true)
  107. -- Testing fgetcsv() with file opened using r+t mode --
  108. int(0)
  109. bool(false)
  110. bool(false)
  111. int(%d)
  112. bool(true)
  113. -- Testing fgetcsv() with file opened using a+ mode --
  114. int(0)
  115. bool(false)
  116. bool(false)
  117. int(53)
  118. bool(true)
  119. -- Testing fgetcsv() with file opened using a+b mode --
  120. int(0)
  121. bool(false)
  122. bool(false)
  123. int(53)
  124. bool(true)
  125. -- Testing fgetcsv() with file opened using a+t mode --
  126. int(0)
  127. bool(false)
  128. bool(false)
  129. int(%d)
  130. bool(true)
  131. -- Testing fgetcsv() with file opened using w+ mode --
  132. int(0)
  133. bool(false)
  134. bool(false)
  135. int(53)
  136. bool(true)
  137. -- Testing fgetcsv() with file opened using w+b mode --
  138. int(0)
  139. bool(false)
  140. bool(false)
  141. int(53)
  142. bool(true)
  143. -- Testing fgetcsv() with file opened using w+t mode --
  144. int(0)
  145. bool(false)
  146. bool(false)
  147. int(%d)
  148. bool(true)
  149. -- Testing fgetcsv() with file opened using x+ mode --
  150. int(0)
  151. bool(false)
  152. bool(false)
  153. int(53)
  154. bool(true)
  155. -- Testing fgetcsv() with file opened using x+b mode --
  156. int(0)
  157. bool(false)
  158. bool(false)
  159. int(53)
  160. bool(true)
  161. -- Testing fgetcsv() with file opened using x+t mode --
  162. int(0)
  163. bool(false)
  164. bool(false)
  165. int(%d)
  166. bool(true)
  167. -- Testing fgetcsv() with file opened using r mode --
  168. int(0)
  169. bool(false)
  170. bool(false)
  171. int(53)
  172. bool(true)
  173. -- Testing fgetcsv() with file opened using rb mode --
  174. int(0)
  175. bool(false)
  176. bool(false)
  177. int(53)
  178. bool(true)
  179. -- Testing fgetcsv() with file opened using rt mode --
  180. int(0)
  181. bool(false)
  182. bool(false)
  183. int(%d)
  184. bool(true)
  185. -- Testing fgetcsv() with file opened using r+ mode --
  186. int(0)
  187. bool(false)
  188. bool(false)
  189. int(53)
  190. bool(true)
  191. -- Testing fgetcsv() with file opened using r+b mode --
  192. int(0)
  193. bool(false)
  194. bool(false)
  195. int(53)
  196. bool(true)
  197. -- Testing fgetcsv() with file opened using r+t mode --
  198. int(0)
  199. bool(false)
  200. bool(false)
  201. int(%d)
  202. bool(true)
  203. -- Testing fgetcsv() with file opened using a+ mode --
  204. int(0)
  205. bool(false)
  206. bool(false)
  207. int(53)
  208. bool(true)
  209. -- Testing fgetcsv() with file opened using a+b mode --
  210. int(0)
  211. bool(false)
  212. bool(false)
  213. int(53)
  214. bool(true)
  215. -- Testing fgetcsv() with file opened using a+t mode --
  216. int(0)
  217. bool(false)
  218. bool(false)
  219. int(%d)
  220. bool(true)
  221. -- Testing fgetcsv() with file opened using w+ mode --
  222. int(0)
  223. bool(false)
  224. bool(false)
  225. int(53)
  226. bool(true)
  227. -- Testing fgetcsv() with file opened using w+b mode --
  228. int(0)
  229. bool(false)
  230. bool(false)
  231. int(53)
  232. bool(true)
  233. -- Testing fgetcsv() with file opened using w+t mode --
  234. int(0)
  235. bool(false)
  236. bool(false)
  237. int(%d)
  238. bool(true)
  239. -- Testing fgetcsv() with file opened using x+ mode --
  240. int(0)
  241. bool(false)
  242. bool(false)
  243. int(53)
  244. bool(true)
  245. -- Testing fgetcsv() with file opened using x+b mode --
  246. int(0)
  247. bool(false)
  248. bool(false)
  249. int(53)
  250. bool(true)
  251. -- Testing fgetcsv() with file opened using x+t mode --
  252. int(0)
  253. bool(false)
  254. bool(false)
  255. int(%d)
  256. bool(true)
  257. -- Testing fgetcsv() with file opened using r mode --
  258. int(0)
  259. bool(false)
  260. bool(false)
  261. int(57)
  262. bool(true)
  263. -- Testing fgetcsv() with file opened using rb mode --
  264. int(0)
  265. bool(false)
  266. bool(false)
  267. int(57)
  268. bool(true)
  269. -- Testing fgetcsv() with file opened using rt mode --
  270. int(0)
  271. bool(false)
  272. bool(false)
  273. int(%d)
  274. bool(true)
  275. -- Testing fgetcsv() with file opened using r+ mode --
  276. int(0)
  277. bool(false)
  278. bool(false)
  279. int(57)
  280. bool(true)
  281. -- Testing fgetcsv() with file opened using r+b mode --
  282. int(0)
  283. bool(false)
  284. bool(false)
  285. int(57)
  286. bool(true)
  287. -- Testing fgetcsv() with file opened using r+t mode --
  288. int(0)
  289. bool(false)
  290. bool(false)
  291. int(%d)
  292. bool(true)
  293. -- Testing fgetcsv() with file opened using a+ mode --
  294. int(0)
  295. bool(false)
  296. bool(false)
  297. int(57)
  298. bool(true)
  299. -- Testing fgetcsv() with file opened using a+b mode --
  300. int(0)
  301. bool(false)
  302. bool(false)
  303. int(57)
  304. bool(true)
  305. -- Testing fgetcsv() with file opened using a+t mode --
  306. int(0)
  307. bool(false)
  308. bool(false)
  309. int(%d)
  310. bool(true)
  311. -- Testing fgetcsv() with file opened using w+ mode --
  312. int(0)
  313. bool(false)
  314. bool(false)
  315. int(57)
  316. bool(true)
  317. -- Testing fgetcsv() with file opened using w+b mode --
  318. int(0)
  319. bool(false)
  320. bool(false)
  321. int(57)
  322. bool(true)
  323. -- Testing fgetcsv() with file opened using w+t mode --
  324. int(0)
  325. bool(false)
  326. bool(false)
  327. int(%d)
  328. bool(true)
  329. -- Testing fgetcsv() with file opened using x+ mode --
  330. int(0)
  331. bool(false)
  332. bool(false)
  333. int(57)
  334. bool(true)
  335. -- Testing fgetcsv() with file opened using x+b mode --
  336. int(0)
  337. bool(false)
  338. bool(false)
  339. int(57)
  340. bool(true)
  341. -- Testing fgetcsv() with file opened using x+t mode --
  342. int(0)
  343. bool(false)
  344. bool(false)
  345. int(%d)
  346. bool(true)
  347. -- Testing fgetcsv() with file opened using r mode --
  348. int(0)
  349. bool(false)
  350. bool(false)
  351. int(61)
  352. bool(true)
  353. -- Testing fgetcsv() with file opened using rb mode --
  354. int(0)
  355. bool(false)
  356. bool(false)
  357. int(61)
  358. bool(true)
  359. -- Testing fgetcsv() with file opened using rt mode --
  360. int(0)
  361. bool(false)
  362. bool(false)
  363. int(%d)
  364. bool(true)
  365. -- Testing fgetcsv() with file opened using r+ mode --
  366. int(0)
  367. bool(false)
  368. bool(false)
  369. int(61)
  370. bool(true)
  371. -- Testing fgetcsv() with file opened using r+b mode --
  372. int(0)
  373. bool(false)
  374. bool(false)
  375. int(61)
  376. bool(true)
  377. -- Testing fgetcsv() with file opened using r+t mode --
  378. int(0)
  379. bool(false)
  380. bool(false)
  381. int(%d)
  382. bool(true)
  383. -- Testing fgetcsv() with file opened using a+ mode --
  384. int(0)
  385. bool(false)
  386. bool(false)
  387. int(61)
  388. bool(true)
  389. -- Testing fgetcsv() with file opened using a+b mode --
  390. int(0)
  391. bool(false)
  392. bool(false)
  393. int(61)
  394. bool(true)
  395. -- Testing fgetcsv() with file opened using a+t mode --
  396. int(0)
  397. bool(false)
  398. bool(false)
  399. int(%d)
  400. bool(true)
  401. -- Testing fgetcsv() with file opened using w+ mode --
  402. int(0)
  403. bool(false)
  404. bool(false)
  405. int(61)
  406. bool(true)
  407. -- Testing fgetcsv() with file opened using w+b mode --
  408. int(0)
  409. bool(false)
  410. bool(false)
  411. int(61)
  412. bool(true)
  413. -- Testing fgetcsv() with file opened using w+t mode --
  414. int(0)
  415. bool(false)
  416. bool(false)
  417. int(%d)
  418. bool(true)
  419. -- Testing fgetcsv() with file opened using x+ mode --
  420. int(0)
  421. bool(false)
  422. bool(false)
  423. int(61)
  424. bool(true)
  425. -- Testing fgetcsv() with file opened using x+b mode --
  426. int(0)
  427. bool(false)
  428. bool(false)
  429. int(61)
  430. bool(true)
  431. -- Testing fgetcsv() with file opened using x+t mode --
  432. int(0)
  433. bool(false)
  434. bool(false)
  435. int(%d)
  436. bool(true)
  437. -- Testing fgetcsv() with file opened using r mode --
  438. int(0)
  439. bool(false)
  440. bool(false)
  441. int(61)
  442. bool(true)
  443. -- Testing fgetcsv() with file opened using rb mode --
  444. int(0)
  445. bool(false)
  446. bool(false)
  447. int(61)
  448. bool(true)
  449. -- Testing fgetcsv() with file opened using rt mode --
  450. int(0)
  451. bool(false)
  452. bool(false)
  453. int(%d)
  454. bool(true)
  455. -- Testing fgetcsv() with file opened using r+ mode --
  456. int(0)
  457. bool(false)
  458. bool(false)
  459. int(61)
  460. bool(true)
  461. -- Testing fgetcsv() with file opened using r+b mode --
  462. int(0)
  463. bool(false)
  464. bool(false)
  465. int(61)
  466. bool(true)
  467. -- Testing fgetcsv() with file opened using r+t mode --
  468. int(0)
  469. bool(false)
  470. bool(false)
  471. int(%d)
  472. bool(true)
  473. -- Testing fgetcsv() with file opened using a+ mode --
  474. int(0)
  475. bool(false)
  476. bool(false)
  477. int(61)
  478. bool(true)
  479. -- Testing fgetcsv() with file opened using a+b mode --
  480. int(0)
  481. bool(false)
  482. bool(false)
  483. int(61)
  484. bool(true)
  485. -- Testing fgetcsv() with file opened using a+t mode --
  486. int(0)
  487. bool(false)
  488. bool(false)
  489. int(%d)
  490. bool(true)
  491. -- Testing fgetcsv() with file opened using w+ mode --
  492. int(0)
  493. bool(false)
  494. bool(false)
  495. int(61)
  496. bool(true)
  497. -- Testing fgetcsv() with file opened using w+b mode --
  498. int(0)
  499. bool(false)
  500. bool(false)
  501. int(61)
  502. bool(true)
  503. -- Testing fgetcsv() with file opened using w+t mode --
  504. int(0)
  505. bool(false)
  506. bool(false)
  507. int(%d)
  508. bool(true)
  509. -- Testing fgetcsv() with file opened using x+ mode --
  510. int(0)
  511. bool(false)
  512. bool(false)
  513. int(61)
  514. bool(true)
  515. -- Testing fgetcsv() with file opened using x+b mode --
  516. int(0)
  517. bool(false)
  518. bool(false)
  519. int(61)
  520. bool(true)
  521. -- Testing fgetcsv() with file opened using x+t mode --
  522. int(0)
  523. bool(false)
  524. bool(false)
  525. int(%d)
  526. bool(true)
  527. Done