fgetcsv_variation21.phpt 3.5 KB

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