fgetcsv_variation14.phpt 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. --TEST--
  2. Test fgetcsv() : usage variations - reading the blank line
  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. /* Testing fgetcsv() by reading a file containing a blank line */
  10. echo "*** Testing fgetcsv() : reading the blank line ***\n";
  11. $filename = dirname(__FILE__) . '/fgetcsv_variation14.tmp';
  12. @unlink($filename);
  13. $file_modes = array ("r","rb", "rt", "r+", "r+b", "r+t",
  14. "a+", "a+b", "a+t",
  15. "w+", "w+b", "w+t",
  16. "x+", "x+b", "x+t");
  17. $loop_counter = 1;
  18. for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
  19. // create the file and add the content with has csv fields
  20. if ( strstr($file_modes[$mode_counter], "r") ) {
  21. $file_handle = fopen($filename, "w");
  22. } else {
  23. $file_handle = fopen($filename, $file_modes[$mode_counter] );
  24. }
  25. if ( !$file_handle ) {
  26. echo "Error: failed to create file $filename!\n";
  27. exit();
  28. }
  29. // write a blank line
  30. fwrite($file_handle, "\n"); // blank line
  31. // close the file if the mode to be used is read mode and re-open using read mode
  32. // else rewind the file pointer to beginning of the file
  33. if ( strstr($file_modes[$mode_counter], "r" ) ) {
  34. fclose($file_handle);
  35. $file_handle = fopen($filename, $file_modes[$mode_counter]);
  36. } else {
  37. // rewind the file pointer to bof
  38. rewind($file_handle);
  39. }
  40. echo "\n-- Testing fgetcsv() with file opened using $file_modes[$mode_counter] mode --\n";
  41. // call fgetcsv() to parse csv fields
  42. // read the next line which is a blank line to see the working of fgetcsv
  43. $fp_pos = ftell($file_handle);
  44. var_dump( fgetcsv($file_handle, 1024) );
  45. // check the file pointer position and if eof
  46. var_dump( ftell($file_handle) );
  47. var_dump( feof($file_handle) );
  48. // read again to struck EOF
  49. var_dump( fgetcsv($file_handle, 1024) );
  50. // check the file pointer position and if eof
  51. var_dump( ftell($file_handle) );
  52. var_dump( feof($file_handle) );
  53. // close the file
  54. fclose($file_handle);
  55. //delete file
  56. unlink($filename);
  57. } //end of mode loop
  58. echo "Done\n";
  59. ?>
  60. --EXPECT--
  61. *** Testing fgetcsv() : reading the blank line ***
  62. -- Testing fgetcsv() with file opened using r mode --
  63. array(1) {
  64. [0]=>
  65. NULL
  66. }
  67. int(1)
  68. bool(false)
  69. bool(false)
  70. int(1)
  71. bool(true)
  72. -- Testing fgetcsv() with file opened using rb mode --
  73. array(1) {
  74. [0]=>
  75. NULL
  76. }
  77. int(1)
  78. bool(false)
  79. bool(false)
  80. int(1)
  81. bool(true)
  82. -- Testing fgetcsv() with file opened using rt mode --
  83. array(1) {
  84. [0]=>
  85. NULL
  86. }
  87. int(1)
  88. bool(false)
  89. bool(false)
  90. int(1)
  91. bool(true)
  92. -- Testing fgetcsv() with file opened using r+ mode --
  93. array(1) {
  94. [0]=>
  95. NULL
  96. }
  97. int(1)
  98. bool(false)
  99. bool(false)
  100. int(1)
  101. bool(true)
  102. -- Testing fgetcsv() with file opened using r+b mode --
  103. array(1) {
  104. [0]=>
  105. NULL
  106. }
  107. int(1)
  108. bool(false)
  109. bool(false)
  110. int(1)
  111. bool(true)
  112. -- Testing fgetcsv() with file opened using r+t mode --
  113. array(1) {
  114. [0]=>
  115. NULL
  116. }
  117. int(1)
  118. bool(false)
  119. bool(false)
  120. int(1)
  121. bool(true)
  122. -- Testing fgetcsv() with file opened using a+ mode --
  123. array(1) {
  124. [0]=>
  125. NULL
  126. }
  127. int(1)
  128. bool(false)
  129. bool(false)
  130. int(1)
  131. bool(true)
  132. -- Testing fgetcsv() with file opened using a+b mode --
  133. array(1) {
  134. [0]=>
  135. NULL
  136. }
  137. int(1)
  138. bool(false)
  139. bool(false)
  140. int(1)
  141. bool(true)
  142. -- Testing fgetcsv() with file opened using a+t mode --
  143. array(1) {
  144. [0]=>
  145. NULL
  146. }
  147. int(1)
  148. bool(false)
  149. bool(false)
  150. int(1)
  151. bool(true)
  152. -- Testing fgetcsv() with file opened using w+ mode --
  153. array(1) {
  154. [0]=>
  155. NULL
  156. }
  157. int(1)
  158. bool(false)
  159. bool(false)
  160. int(1)
  161. bool(true)
  162. -- Testing fgetcsv() with file opened using w+b mode --
  163. array(1) {
  164. [0]=>
  165. NULL
  166. }
  167. int(1)
  168. bool(false)
  169. bool(false)
  170. int(1)
  171. bool(true)
  172. -- Testing fgetcsv() with file opened using w+t mode --
  173. array(1) {
  174. [0]=>
  175. NULL
  176. }
  177. int(1)
  178. bool(false)
  179. bool(false)
  180. int(1)
  181. bool(true)
  182. -- Testing fgetcsv() with file opened using x+ mode --
  183. array(1) {
  184. [0]=>
  185. NULL
  186. }
  187. int(1)
  188. bool(false)
  189. bool(false)
  190. int(1)
  191. bool(true)
  192. -- Testing fgetcsv() with file opened using x+b mode --
  193. array(1) {
  194. [0]=>
  195. NULL
  196. }
  197. int(1)
  198. bool(false)
  199. bool(false)
  200. int(1)
  201. bool(true)
  202. -- Testing fgetcsv() with file opened using x+t mode --
  203. array(1) {
  204. [0]=>
  205. NULL
  206. }
  207. int(1)
  208. bool(false)
  209. bool(false)
  210. int(1)
  211. bool(true)
  212. Done