fwrite_variation4.phpt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. --TEST--
  2. Test fwrite() function : usage variations - x, xb, xt, x+, x+b & x+t modes
  3. --SKIPIF--
  4. <?php
  5. if( substr(PHP_OS, 0, 3) == 'WIN' ) {
  6. die('skip...Not valid for Windows');
  7. }
  8. ?>
  9. --FILE--
  10. <?php
  11. /*
  12. Prototype: int fwrite ( resource $handle,string string, [, int $length] );
  13. Description: fwrite() writes the contents of string to the file stream pointed to by handle.
  14. If the length arquement is given,writing will stop after length bytes have been
  15. written or the end of string reached, whichever comes first.
  16. fwrite() returns the number of bytes written or FALSE on error
  17. */
  18. echo "*** Testing fwrite() various operations ***\n";
  19. // include the file.inc for Function: function delete_file($filename)
  20. include ("file.inc");
  21. /*
  22. Test fwrite with file opened in mode : x, xb, xt, x+, x+b, x+t
  23. File having content of type numeric, text,text_with_new_line & alphanumeric
  24. */
  25. $file_modes = array("x","xb","xt","x+","x+b","x+t");
  26. $file_content_types = array("numeric","text","text_with_new_line","alphanumeric");
  27. foreach($file_content_types as $file_content_type) {
  28. echo "\n-- Testing fwrite() with file having content of type ". $file_content_type ." --\n";
  29. /* open the file using $files_modes and perform fwrite() on it */
  30. foreach($file_modes as $file_mode) {
  31. echo "-- Opening file in $file_mode --\n";
  32. $filename = dirname(__FILE__)."/fwrite_variation4.tmp"; // this is name of the file
  33. $file_handle = fopen($filename, $file_mode);
  34. if(!$file_handle) {
  35. echo "Error: failed to fopen() file: $filename!";
  36. exit();
  37. }
  38. $data_to_be_written="";
  39. fill_buffer($data_to_be_written,$file_content_type,1024); //get the data of size 1024
  40. /* Write the data into the file, verify it by checking the file pointer position, eof position,
  41. filesize & by displaying the content */
  42. // write data to the file
  43. var_dump( ftell($file_handle) );
  44. var_dump( fwrite($file_handle,$data_to_be_written,400));
  45. var_dump( ftell($file_handle) );
  46. var_dump( feof($file_handle) ); // expected: true
  47. //check the filesize and content
  48. // close the file, get the size and content of the file.
  49. var_dump( fclose($file_handle) );
  50. clearstatcache();//clears file status cache
  51. var_dump( filesize($filename) );
  52. var_dump(md5(file_get_contents($filename)));
  53. // delete the file created
  54. delete_file($filename); // delete file with name fwrite_variation4.tmp
  55. } // end of inner foreach loop
  56. } // end of outer foreach loop
  57. echo "Done\n";
  58. ?>
  59. --EXPECTF--
  60. *** Testing fwrite() various operations ***
  61. -- Testing fwrite() with file having content of type numeric --
  62. -- Opening file in x --
  63. int(0)
  64. int(400)
  65. int(400)
  66. bool(false)
  67. bool(true)
  68. int(400)
  69. string(32) "f255efe87ebdf755e515868cea9ad24b"
  70. -- Opening file in xb --
  71. int(0)
  72. int(400)
  73. int(400)
  74. bool(false)
  75. bool(true)
  76. int(400)
  77. string(32) "f255efe87ebdf755e515868cea9ad24b"
  78. -- Opening file in xt --
  79. int(0)
  80. int(400)
  81. int(400)
  82. bool(false)
  83. bool(true)
  84. int(400)
  85. string(32) "f255efe87ebdf755e515868cea9ad24b"
  86. -- Opening file in x+ --
  87. int(0)
  88. int(400)
  89. int(400)
  90. bool(false)
  91. bool(true)
  92. int(400)
  93. string(32) "f255efe87ebdf755e515868cea9ad24b"
  94. -- Opening file in x+b --
  95. int(0)
  96. int(400)
  97. int(400)
  98. bool(false)
  99. bool(true)
  100. int(400)
  101. string(32) "f255efe87ebdf755e515868cea9ad24b"
  102. -- Opening file in x+t --
  103. int(0)
  104. int(400)
  105. int(400)
  106. bool(false)
  107. bool(true)
  108. int(400)
  109. string(32) "f255efe87ebdf755e515868cea9ad24b"
  110. -- Testing fwrite() with file having content of type text --
  111. -- Opening file in x --
  112. int(0)
  113. int(400)
  114. int(400)
  115. bool(false)
  116. bool(true)
  117. int(400)
  118. string(32) "c2244282eeca7c2d32d0dacf21e19432"
  119. -- Opening file in xb --
  120. int(0)
  121. int(400)
  122. int(400)
  123. bool(false)
  124. bool(true)
  125. int(400)
  126. string(32) "c2244282eeca7c2d32d0dacf21e19432"
  127. -- Opening file in xt --
  128. int(0)
  129. int(400)
  130. int(400)
  131. bool(false)
  132. bool(true)
  133. int(400)
  134. string(32) "c2244282eeca7c2d32d0dacf21e19432"
  135. -- Opening file in x+ --
  136. int(0)
  137. int(400)
  138. int(400)
  139. bool(false)
  140. bool(true)
  141. int(400)
  142. string(32) "c2244282eeca7c2d32d0dacf21e19432"
  143. -- Opening file in x+b --
  144. int(0)
  145. int(400)
  146. int(400)
  147. bool(false)
  148. bool(true)
  149. int(400)
  150. string(32) "c2244282eeca7c2d32d0dacf21e19432"
  151. -- Opening file in x+t --
  152. int(0)
  153. int(400)
  154. int(400)
  155. bool(false)
  156. bool(true)
  157. int(400)
  158. string(32) "c2244282eeca7c2d32d0dacf21e19432"
  159. -- Testing fwrite() with file having content of type text_with_new_line --
  160. -- Opening file in x --
  161. int(0)
  162. int(400)
  163. int(400)
  164. bool(false)
  165. bool(true)
  166. int(400)
  167. string(32) "fa6c79b925c2fc754b9d063c6de1d8df"
  168. -- Opening file in xb --
  169. int(0)
  170. int(400)
  171. int(400)
  172. bool(false)
  173. bool(true)
  174. int(400)
  175. string(32) "fa6c79b925c2fc754b9d063c6de1d8df"
  176. -- Opening file in xt --
  177. int(0)
  178. int(400)
  179. int(400)
  180. bool(false)
  181. bool(true)
  182. int(400)
  183. string(32) "fa6c79b925c2fc754b9d063c6de1d8df"
  184. -- Opening file in x+ --
  185. int(0)
  186. int(400)
  187. int(400)
  188. bool(false)
  189. bool(true)
  190. int(400)
  191. string(32) "fa6c79b925c2fc754b9d063c6de1d8df"
  192. -- Opening file in x+b --
  193. int(0)
  194. int(400)
  195. int(400)
  196. bool(false)
  197. bool(true)
  198. int(400)
  199. string(32) "fa6c79b925c2fc754b9d063c6de1d8df"
  200. -- Opening file in x+t --
  201. int(0)
  202. int(400)
  203. int(400)
  204. bool(false)
  205. bool(true)
  206. int(400)
  207. string(32) "fa6c79b925c2fc754b9d063c6de1d8df"
  208. -- Testing fwrite() with file having content of type alphanumeric --
  209. -- Opening file in x --
  210. int(0)
  211. int(400)
  212. int(400)
  213. bool(false)
  214. bool(true)
  215. int(400)
  216. string(32) "b2a123e1d84e6a03c8520aff7689219e"
  217. -- Opening file in xb --
  218. int(0)
  219. int(400)
  220. int(400)
  221. bool(false)
  222. bool(true)
  223. int(400)
  224. string(32) "b2a123e1d84e6a03c8520aff7689219e"
  225. -- Opening file in xt --
  226. int(0)
  227. int(400)
  228. int(400)
  229. bool(false)
  230. bool(true)
  231. int(400)
  232. string(32) "b2a123e1d84e6a03c8520aff7689219e"
  233. -- Opening file in x+ --
  234. int(0)
  235. int(400)
  236. int(400)
  237. bool(false)
  238. bool(true)
  239. int(400)
  240. string(32) "b2a123e1d84e6a03c8520aff7689219e"
  241. -- Opening file in x+b --
  242. int(0)
  243. int(400)
  244. int(400)
  245. bool(false)
  246. bool(true)
  247. int(400)
  248. string(32) "b2a123e1d84e6a03c8520aff7689219e"
  249. -- Opening file in x+t --
  250. int(0)
  251. int(400)
  252. int(400)
  253. bool(false)
  254. bool(true)
  255. int(400)
  256. string(32) "b2a123e1d84e6a03c8520aff7689219e"
  257. Done