fgetcsv_variation22.phpt 11 KB

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