fgetcsv_variation14.phpt 4.1 KB

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