fgetcsv_variation21.phpt 3.7 KB

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