fputcsv_variation10.phpt 4.7 KB

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