fputcsv_variation10.phpt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. --TEST--
  2. Test fputcsv() : usage variations - with line without any csv fields
  3. --FILE--
  4. <?php
  5. /* Testing fputcsv() to write to a file when the field has no CSV format */
  6. echo "*** Testing fputcsv() : with no CSV format in the field ***\n";
  7. /* the array is with three elements in it. Each element should be read as
  8. 1st element is delimiter, 2nd element is enclosure
  9. and 3rd element is csv fields
  10. */
  11. $fields = array( array('water_fruit\n'),
  12. array("water_fruit\n"),
  13. array("")
  14. );
  15. $file_path = __DIR__;
  16. $filename = "$file_path/fputcsv_variation10.tmp";
  17. $file_modes = array ("r+", "r+b", "r+t",
  18. "a+", "a+b", "a+t",
  19. "w+", "w+b", "w+t",
  20. "x+", "x+b", "x+t");
  21. $loop_counter = 1;
  22. foreach ($fields as $field) {
  23. for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
  24. echo "\n-- file opened in $file_modes[$mode_counter] --\n";
  25. // create the file and add the content with has csv fields
  26. if ( strstr($file_modes[$mode_counter], "r") ) {
  27. $file_handle = fopen($filename, "w");
  28. } else {
  29. $file_handle = fopen($filename, $file_modes[$mode_counter] );
  30. }
  31. if ( !$file_handle ) {
  32. echo "Error: failed to create file $filename!\n";
  33. exit();
  34. }
  35. $csv_field = $field;
  36. // write to a file in csv format
  37. var_dump( fputcsv($file_handle, $csv_field) );
  38. // check the file pointer position and eof
  39. var_dump( ftell($file_handle) );
  40. var_dump( feof($file_handle) );
  41. //close the file
  42. fclose($file_handle);
  43. // print the file contents
  44. var_dump( file_get_contents($filename) );
  45. //delete file
  46. unlink($filename);
  47. } //end of mode loop
  48. } // end of foreach
  49. echo "Done\n";
  50. ?>
  51. --EXPECTF--
  52. *** Testing fputcsv() : with no CSV format in the field ***
  53. -- file opened in r+ --
  54. int(16)
  55. int(16)
  56. bool(false)
  57. string(16) ""water_fruit\n"
  58. "
  59. -- file opened in r+b --
  60. int(16)
  61. int(16)
  62. bool(false)
  63. string(16) ""water_fruit\n"
  64. "
  65. -- file opened in r+t --
  66. int(16)
  67. int(16)
  68. bool(false)
  69. string(%d) ""water_fruit\n"
  70. "
  71. -- file opened in a+ --
  72. int(16)
  73. int(16)
  74. bool(false)
  75. string(16) ""water_fruit\n"
  76. "
  77. -- file opened in a+b --
  78. int(16)
  79. int(16)
  80. bool(false)
  81. string(16) ""water_fruit\n"
  82. "
  83. -- file opened in a+t --
  84. int(16)
  85. int(16)
  86. bool(false)
  87. string(%d) ""water_fruit\n"
  88. "
  89. -- file opened in w+ --
  90. int(16)
  91. int(16)
  92. bool(false)
  93. string(16) ""water_fruit\n"
  94. "
  95. -- file opened in w+b --
  96. int(16)
  97. int(16)
  98. bool(false)
  99. string(16) ""water_fruit\n"
  100. "
  101. -- file opened in w+t --
  102. int(16)
  103. int(16)
  104. bool(false)
  105. string(%d) ""water_fruit\n"
  106. "
  107. -- file opened in x+ --
  108. int(16)
  109. int(16)
  110. bool(false)
  111. string(16) ""water_fruit\n"
  112. "
  113. -- file opened in x+b --
  114. int(16)
  115. int(16)
  116. bool(false)
  117. string(16) ""water_fruit\n"
  118. "
  119. -- file opened in x+t --
  120. int(16)
  121. int(16)
  122. bool(false)
  123. string(%d) ""water_fruit\n"
  124. "
  125. -- file opened in r+ --
  126. int(15)
  127. int(15)
  128. bool(false)
  129. string(15) ""water_fruit
  130. "
  131. "
  132. -- file opened in r+b --
  133. int(15)
  134. int(15)
  135. bool(false)
  136. string(15) ""water_fruit
  137. "
  138. "
  139. -- file opened in r+t --
  140. int(15)
  141. int(15)
  142. bool(false)
  143. string(%d) ""water_fruit
  144. "
  145. "
  146. -- file opened in a+ --
  147. int(15)
  148. int(15)
  149. bool(false)
  150. string(15) ""water_fruit
  151. "
  152. "
  153. -- file opened in a+b --
  154. int(15)
  155. int(15)
  156. bool(false)
  157. string(15) ""water_fruit
  158. "
  159. "
  160. -- file opened in a+t --
  161. int(15)
  162. int(15)
  163. bool(false)
  164. string(%d) ""water_fruit
  165. "
  166. "
  167. -- file opened in w+ --
  168. int(15)
  169. int(15)
  170. bool(false)
  171. string(15) ""water_fruit
  172. "
  173. "
  174. -- file opened in w+b --
  175. int(15)
  176. int(15)
  177. bool(false)
  178. string(15) ""water_fruit
  179. "
  180. "
  181. -- file opened in w+t --
  182. int(15)
  183. int(15)
  184. bool(false)
  185. string(%d) ""water_fruit
  186. "
  187. "
  188. -- file opened in x+ --
  189. int(15)
  190. int(15)
  191. bool(false)
  192. string(15) ""water_fruit
  193. "
  194. "
  195. -- file opened in x+b --
  196. int(15)
  197. int(15)
  198. bool(false)
  199. string(15) ""water_fruit
  200. "
  201. "
  202. -- file opened in x+t --
  203. int(15)
  204. int(15)
  205. bool(false)
  206. string(%d) ""water_fruit
  207. "
  208. "
  209. -- file opened in r+ --
  210. int(1)
  211. int(1)
  212. bool(false)
  213. string(1) "
  214. "
  215. -- file opened in r+b --
  216. int(1)
  217. int(1)
  218. bool(false)
  219. string(1) "
  220. "
  221. -- file opened in r+t --
  222. int(1)
  223. int(1)
  224. bool(false)
  225. string(%d) "
  226. "
  227. -- file opened in a+ --
  228. int(1)
  229. int(1)
  230. bool(false)
  231. string(1) "
  232. "
  233. -- file opened in a+b --
  234. int(1)
  235. int(1)
  236. bool(false)
  237. string(1) "
  238. "
  239. -- file opened in a+t --
  240. int(1)
  241. int(1)
  242. bool(false)
  243. string(%d) "
  244. "
  245. -- file opened in w+ --
  246. int(1)
  247. int(1)
  248. bool(false)
  249. string(1) "
  250. "
  251. -- file opened in w+b --
  252. int(1)
  253. int(1)
  254. bool(false)
  255. string(1) "
  256. "
  257. -- file opened in w+t --
  258. int(1)
  259. int(1)
  260. bool(false)
  261. string(%d) "
  262. "
  263. -- file opened in x+ --
  264. int(1)
  265. int(1)
  266. bool(false)
  267. string(1) "
  268. "
  269. -- file opened in x+b --
  270. int(1)
  271. int(1)
  272. bool(false)
  273. string(1) "
  274. "
  275. -- file opened in x+t --
  276. int(1)
  277. int(1)
  278. bool(false)
  279. string(%d) "
  280. "
  281. Done