fgetcsv_variation31.phpt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. --TEST--
  2. Test fgetcsv() : usage variations - with length and enclosure, 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 enclosure argument
  12. */
  13. echo "*** Testing fgetcsv() : with enclosure argument, 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_variation31.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. $enc = 'z';
  64. var_dump( fgetcsv($file_handle, 1024, $delimiter, $enc ) ); // with length, delimiter
  65. // check the file pointer position and if eof
  66. var_dump( ftell($file_handle) );
  67. var_dump( feof($file_handle) );
  68. // close the file
  69. fclose($file_handle);
  70. //delete file
  71. unlink($filename);
  72. } //end of mode loop
  73. } // end of foreach
  74. echo "Done\n";
  75. ?>
  76. --EXPECTF--
  77. *** Testing fgetcsv() : with enclosure argument, file pointer pointing at end of file ***
  78. -- Testing fgetcsv() with file opened using r mode --
  79. int(0)
  80. bool(false)
  81. bool(false)
  82. int(53)
  83. bool(true)
  84. -- Testing fgetcsv() with file opened using rb mode --
  85. int(0)
  86. bool(false)
  87. bool(false)
  88. int(53)
  89. bool(true)
  90. -- Testing fgetcsv() with file opened using rt mode --
  91. int(0)
  92. bool(false)
  93. bool(false)
  94. int(%d)
  95. bool(true)
  96. -- Testing fgetcsv() with file opened using r+ mode --
  97. int(0)
  98. bool(false)
  99. bool(false)
  100. int(53)
  101. bool(true)
  102. -- Testing fgetcsv() with file opened using r+b mode --
  103. int(0)
  104. bool(false)
  105. bool(false)
  106. int(53)
  107. bool(true)
  108. -- Testing fgetcsv() with file opened using r+t mode --
  109. int(0)
  110. bool(false)
  111. bool(false)
  112. int(%d)
  113. bool(true)
  114. -- Testing fgetcsv() with file opened using a+ mode --
  115. int(0)
  116. bool(false)
  117. bool(false)
  118. int(53)
  119. bool(true)
  120. -- Testing fgetcsv() with file opened using a+b mode --
  121. int(0)
  122. bool(false)
  123. bool(false)
  124. int(53)
  125. bool(true)
  126. -- Testing fgetcsv() with file opened using a+t mode --
  127. int(0)
  128. bool(false)
  129. bool(false)
  130. int(%d)
  131. bool(true)
  132. -- Testing fgetcsv() with file opened using w+ mode --
  133. int(0)
  134. bool(false)
  135. bool(false)
  136. int(53)
  137. bool(true)
  138. -- Testing fgetcsv() with file opened using w+b mode --
  139. int(0)
  140. bool(false)
  141. bool(false)
  142. int(53)
  143. bool(true)
  144. -- Testing fgetcsv() with file opened using w+t mode --
  145. int(0)
  146. bool(false)
  147. bool(false)
  148. int(%d)
  149. bool(true)
  150. -- Testing fgetcsv() with file opened using x+ mode --
  151. int(0)
  152. bool(false)
  153. bool(false)
  154. int(53)
  155. bool(true)
  156. -- Testing fgetcsv() with file opened using x+b mode --
  157. int(0)
  158. bool(false)
  159. bool(false)
  160. int(53)
  161. bool(true)
  162. -- Testing fgetcsv() with file opened using x+t mode --
  163. int(0)
  164. bool(false)
  165. bool(false)
  166. int(%d)
  167. bool(true)
  168. -- Testing fgetcsv() with file opened using r mode --
  169. int(0)
  170. bool(false)
  171. bool(false)
  172. int(53)
  173. bool(true)
  174. -- Testing fgetcsv() with file opened using rb mode --
  175. int(0)
  176. bool(false)
  177. bool(false)
  178. int(53)
  179. bool(true)
  180. -- Testing fgetcsv() with file opened using rt mode --
  181. int(0)
  182. bool(false)
  183. bool(false)
  184. int(%d)
  185. bool(true)
  186. -- Testing fgetcsv() with file opened using r+ mode --
  187. int(0)
  188. bool(false)
  189. bool(false)
  190. int(53)
  191. bool(true)
  192. -- Testing fgetcsv() with file opened using r+b mode --
  193. int(0)
  194. bool(false)
  195. bool(false)
  196. int(53)
  197. bool(true)
  198. -- Testing fgetcsv() with file opened using r+t mode --
  199. int(0)
  200. bool(false)
  201. bool(false)
  202. int(%d)
  203. bool(true)
  204. -- Testing fgetcsv() with file opened using a+ mode --
  205. int(0)
  206. bool(false)
  207. bool(false)
  208. int(53)
  209. bool(true)
  210. -- Testing fgetcsv() with file opened using a+b mode --
  211. int(0)
  212. bool(false)
  213. bool(false)
  214. int(53)
  215. bool(true)
  216. -- Testing fgetcsv() with file opened using a+t mode --
  217. int(0)
  218. bool(false)
  219. bool(false)
  220. int(%d)
  221. bool(true)
  222. -- Testing fgetcsv() with file opened using w+ mode --
  223. int(0)
  224. bool(false)
  225. bool(false)
  226. int(53)
  227. bool(true)
  228. -- Testing fgetcsv() with file opened using w+b mode --
  229. int(0)
  230. bool(false)
  231. bool(false)
  232. int(53)
  233. bool(true)
  234. -- Testing fgetcsv() with file opened using w+t mode --
  235. int(0)
  236. bool(false)
  237. bool(false)
  238. int(%d)
  239. bool(true)
  240. -- Testing fgetcsv() with file opened using x+ mode --
  241. int(0)
  242. bool(false)
  243. bool(false)
  244. int(53)
  245. bool(true)
  246. -- Testing fgetcsv() with file opened using x+b mode --
  247. int(0)
  248. bool(false)
  249. bool(false)
  250. int(53)
  251. bool(true)
  252. -- Testing fgetcsv() with file opened using x+t mode --
  253. int(0)
  254. bool(false)
  255. bool(false)
  256. int(%d)
  257. bool(true)
  258. -- Testing fgetcsv() with file opened using r mode --
  259. int(0)
  260. bool(false)
  261. bool(false)
  262. int(57)
  263. bool(true)
  264. -- Testing fgetcsv() with file opened using rb mode --
  265. int(0)
  266. bool(false)
  267. bool(false)
  268. int(57)
  269. bool(true)
  270. -- Testing fgetcsv() with file opened using rt mode --
  271. int(0)
  272. bool(false)
  273. bool(false)
  274. int(%d)
  275. bool(true)
  276. -- Testing fgetcsv() with file opened using r+ mode --
  277. int(0)
  278. bool(false)
  279. bool(false)
  280. int(57)
  281. bool(true)
  282. -- Testing fgetcsv() with file opened using r+b mode --
  283. int(0)
  284. bool(false)
  285. bool(false)
  286. int(57)
  287. bool(true)
  288. -- Testing fgetcsv() with file opened using r+t mode --
  289. int(0)
  290. bool(false)
  291. bool(false)
  292. int(%d)
  293. bool(true)
  294. -- Testing fgetcsv() with file opened using a+ mode --
  295. int(0)
  296. bool(false)
  297. bool(false)
  298. int(57)
  299. bool(true)
  300. -- Testing fgetcsv() with file opened using a+b mode --
  301. int(0)
  302. bool(false)
  303. bool(false)
  304. int(57)
  305. bool(true)
  306. -- Testing fgetcsv() with file opened using a+t mode --
  307. int(0)
  308. bool(false)
  309. bool(false)
  310. int(%d)
  311. bool(true)
  312. -- Testing fgetcsv() with file opened using w+ mode --
  313. int(0)
  314. bool(false)
  315. bool(false)
  316. int(57)
  317. bool(true)
  318. -- Testing fgetcsv() with file opened using w+b mode --
  319. int(0)
  320. bool(false)
  321. bool(false)
  322. int(57)
  323. bool(true)
  324. -- Testing fgetcsv() with file opened using w+t mode --
  325. int(0)
  326. bool(false)
  327. bool(false)
  328. int(%d)
  329. bool(true)
  330. -- Testing fgetcsv() with file opened using x+ mode --
  331. int(0)
  332. bool(false)
  333. bool(false)
  334. int(57)
  335. bool(true)
  336. -- Testing fgetcsv() with file opened using x+b mode --
  337. int(0)
  338. bool(false)
  339. bool(false)
  340. int(57)
  341. bool(true)
  342. -- Testing fgetcsv() with file opened using x+t mode --
  343. int(0)
  344. bool(false)
  345. bool(false)
  346. int(%d)
  347. bool(true)
  348. -- Testing fgetcsv() with file opened using r mode --
  349. int(0)
  350. bool(false)
  351. bool(false)
  352. int(61)
  353. bool(true)
  354. -- Testing fgetcsv() with file opened using rb mode --
  355. int(0)
  356. bool(false)
  357. bool(false)
  358. int(61)
  359. bool(true)
  360. -- Testing fgetcsv() with file opened using rt mode --
  361. int(0)
  362. bool(false)
  363. bool(false)
  364. int(%d)
  365. bool(true)
  366. -- Testing fgetcsv() with file opened using r+ mode --
  367. int(0)
  368. bool(false)
  369. bool(false)
  370. int(61)
  371. bool(true)
  372. -- Testing fgetcsv() with file opened using r+b mode --
  373. int(0)
  374. bool(false)
  375. bool(false)
  376. int(61)
  377. bool(true)
  378. -- Testing fgetcsv() with file opened using r+t mode --
  379. int(0)
  380. bool(false)
  381. bool(false)
  382. int(%d)
  383. bool(true)
  384. -- Testing fgetcsv() with file opened using a+ mode --
  385. int(0)
  386. bool(false)
  387. bool(false)
  388. int(61)
  389. bool(true)
  390. -- Testing fgetcsv() with file opened using a+b mode --
  391. int(0)
  392. bool(false)
  393. bool(false)
  394. int(61)
  395. bool(true)
  396. -- Testing fgetcsv() with file opened using a+t mode --
  397. int(0)
  398. bool(false)
  399. bool(false)
  400. int(%d)
  401. bool(true)
  402. -- Testing fgetcsv() with file opened using w+ mode --
  403. int(0)
  404. bool(false)
  405. bool(false)
  406. int(61)
  407. bool(true)
  408. -- Testing fgetcsv() with file opened using w+b mode --
  409. int(0)
  410. bool(false)
  411. bool(false)
  412. int(61)
  413. bool(true)
  414. -- Testing fgetcsv() with file opened using w+t mode --
  415. int(0)
  416. bool(false)
  417. bool(false)
  418. int(%d)
  419. bool(true)
  420. -- Testing fgetcsv() with file opened using x+ mode --
  421. int(0)
  422. bool(false)
  423. bool(false)
  424. int(61)
  425. bool(true)
  426. -- Testing fgetcsv() with file opened using x+b mode --
  427. int(0)
  428. bool(false)
  429. bool(false)
  430. int(61)
  431. bool(true)
  432. -- Testing fgetcsv() with file opened using x+t mode --
  433. int(0)
  434. bool(false)
  435. bool(false)
  436. int(%d)
  437. bool(true)
  438. -- Testing fgetcsv() with file opened using r mode --
  439. int(0)
  440. bool(false)
  441. bool(false)
  442. int(61)
  443. bool(true)
  444. -- Testing fgetcsv() with file opened using rb mode --
  445. int(0)
  446. bool(false)
  447. bool(false)
  448. int(61)
  449. bool(true)
  450. -- Testing fgetcsv() with file opened using rt mode --
  451. int(0)
  452. bool(false)
  453. bool(false)
  454. int(%d)
  455. bool(true)
  456. -- Testing fgetcsv() with file opened using r+ mode --
  457. int(0)
  458. bool(false)
  459. bool(false)
  460. int(61)
  461. bool(true)
  462. -- Testing fgetcsv() with file opened using r+b mode --
  463. int(0)
  464. bool(false)
  465. bool(false)
  466. int(61)
  467. bool(true)
  468. -- Testing fgetcsv() with file opened using r+t mode --
  469. int(0)
  470. bool(false)
  471. bool(false)
  472. int(%d)
  473. bool(true)
  474. -- Testing fgetcsv() with file opened using a+ mode --
  475. int(0)
  476. bool(false)
  477. bool(false)
  478. int(61)
  479. bool(true)
  480. -- Testing fgetcsv() with file opened using a+b mode --
  481. int(0)
  482. bool(false)
  483. bool(false)
  484. int(61)
  485. bool(true)
  486. -- Testing fgetcsv() with file opened using a+t mode --
  487. int(0)
  488. bool(false)
  489. bool(false)
  490. int(%d)
  491. bool(true)
  492. -- Testing fgetcsv() with file opened using w+ mode --
  493. int(0)
  494. bool(false)
  495. bool(false)
  496. int(61)
  497. bool(true)
  498. -- Testing fgetcsv() with file opened using w+b mode --
  499. int(0)
  500. bool(false)
  501. bool(false)
  502. int(61)
  503. bool(true)
  504. -- Testing fgetcsv() with file opened using w+t mode --
  505. int(0)
  506. bool(false)
  507. bool(false)
  508. int(%d)
  509. bool(true)
  510. -- Testing fgetcsv() with file opened using x+ mode --
  511. int(0)
  512. bool(false)
  513. bool(false)
  514. int(61)
  515. bool(true)
  516. -- Testing fgetcsv() with file opened using x+b mode --
  517. int(0)
  518. bool(false)
  519. bool(false)
  520. int(61)
  521. bool(true)
  522. -- Testing fgetcsv() with file opened using x+t mode --
  523. int(0)
  524. bool(false)
  525. bool(false)
  526. int(%d)
  527. bool(true)
  528. Done