fgetcsv_variation13.phpt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. --TEST--
  2. Test fgetcsv() : usage variations - with line without any csv fields
  3. --FILE--
  4. <?php
  5. /* Testing fgetcsv() to read a line from a file which doesn't have any CSV field */
  6. echo "*** Testing fgetcsv() : reading the line which is without csv fields ***\n";
  7. $filename = __DIR__ . '/fgetcsv_variation13.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 line of text
  26. fwrite($file_handle, "This is line of text without csv fields\n");
  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. // read the line which is without csv fields, provide delimiter and see the working of fgetcsv
  38. $fp_pos = ftell($file_handle);
  39. var_dump( fgetcsv($file_handle) );
  40. // check the file pointer position and if eof
  41. var_dump( ftell($file_handle) );
  42. var_dump( feof($file_handle) );
  43. // close the file
  44. fclose($file_handle);
  45. //delete file
  46. unlink($filename);
  47. } //end of mode loop
  48. echo "Done\n";
  49. ?>
  50. --EXPECT--
  51. *** Testing fgetcsv() : reading the line which is without csv fields ***
  52. -- Testing fgetcsv() with file opened using r mode --
  53. array(1) {
  54. [0]=>
  55. string(39) "This is line of text without csv fields"
  56. }
  57. int(40)
  58. bool(false)
  59. -- Testing fgetcsv() with file opened using rb mode --
  60. array(1) {
  61. [0]=>
  62. string(39) "This is line of text without csv fields"
  63. }
  64. int(40)
  65. bool(false)
  66. -- Testing fgetcsv() with file opened using rt mode --
  67. array(1) {
  68. [0]=>
  69. string(39) "This is line of text without csv fields"
  70. }
  71. int(40)
  72. bool(false)
  73. -- Testing fgetcsv() with file opened using r+ mode --
  74. array(1) {
  75. [0]=>
  76. string(39) "This is line of text without csv fields"
  77. }
  78. int(40)
  79. bool(false)
  80. -- Testing fgetcsv() with file opened using r+b mode --
  81. array(1) {
  82. [0]=>
  83. string(39) "This is line of text without csv fields"
  84. }
  85. int(40)
  86. bool(false)
  87. -- Testing fgetcsv() with file opened using r+t mode --
  88. array(1) {
  89. [0]=>
  90. string(39) "This is line of text without csv fields"
  91. }
  92. int(40)
  93. bool(false)
  94. -- Testing fgetcsv() with file opened using a+ mode --
  95. array(1) {
  96. [0]=>
  97. string(39) "This is line of text without csv fields"
  98. }
  99. int(40)
  100. bool(false)
  101. -- Testing fgetcsv() with file opened using a+b mode --
  102. array(1) {
  103. [0]=>
  104. string(39) "This is line of text without csv fields"
  105. }
  106. int(40)
  107. bool(false)
  108. -- Testing fgetcsv() with file opened using a+t mode --
  109. array(1) {
  110. [0]=>
  111. string(39) "This is line of text without csv fields"
  112. }
  113. int(40)
  114. bool(false)
  115. -- Testing fgetcsv() with file opened using w+ mode --
  116. array(1) {
  117. [0]=>
  118. string(39) "This is line of text without csv fields"
  119. }
  120. int(40)
  121. bool(false)
  122. -- Testing fgetcsv() with file opened using w+b mode --
  123. array(1) {
  124. [0]=>
  125. string(39) "This is line of text without csv fields"
  126. }
  127. int(40)
  128. bool(false)
  129. -- Testing fgetcsv() with file opened using w+t mode --
  130. array(1) {
  131. [0]=>
  132. string(39) "This is line of text without csv fields"
  133. }
  134. int(40)
  135. bool(false)
  136. -- Testing fgetcsv() with file opened using x+ mode --
  137. array(1) {
  138. [0]=>
  139. string(39) "This is line of text without csv fields"
  140. }
  141. int(40)
  142. bool(false)
  143. -- Testing fgetcsv() with file opened using x+b mode --
  144. array(1) {
  145. [0]=>
  146. string(39) "This is line of text without csv fields"
  147. }
  148. int(40)
  149. bool(false)
  150. -- Testing fgetcsv() with file opened using x+t mode --
  151. array(1) {
  152. [0]=>
  153. string(39) "This is line of text without csv fields"
  154. }
  155. int(40)
  156. bool(false)
  157. Done