fgetcsv_variation31.phpt 11 KB

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