fwrite_variation2.phpt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. --TEST--
  2. Test fwrite() function : usage variations - r+, r+b & r+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. echo "*** Testing fwrite() various operations ***\n";
  12. // include the file.inc for Function: function delete_file($filename)
  13. include ("file.inc");
  14. /*
  15. Test fwrite with file opened in mode : r+,r+b,r+t
  16. File having content of type numeric, text,text_with_new_line & alphanumeric
  17. */
  18. $file_modes = array("r+", "r+b", "r+t");
  19. $file_content_types = array("numeric","text","text_with_new_line","alphanumeric");
  20. foreach($file_content_types as $file_content_type) {
  21. echo "\n-- Testing fwrite() with file having content of type ". $file_content_type ." --\n";
  22. /* open the file using $files_modes and perform fwrite() on it */
  23. foreach($file_modes as $file_mode) {
  24. echo "-- Opening file in $file_mode --\n";
  25. // create temp file and fill the data of type $file_content_type
  26. $filename = __DIR__."/fwrite_variation2.tmp"; // this is name of the file
  27. create_files ( __DIR__, 1, $file_content_type, 0755, 1, "w", "fwrite_variation", 2);
  28. $file_handle = fopen($filename, $file_mode);
  29. if(!$file_handle) {
  30. echo "Error: failed to fopen() file: $filename!";
  31. exit();
  32. }
  33. $data_to_be_written="";
  34. fill_buffer($data_to_be_written,$file_content_type,1024); //get the data of size 1024
  35. /* Write the data into the file, verify it by checking the file pointer position, eof position,
  36. filesize & by displaying the content */
  37. /*overwrite first 400 bytes in the file*/
  38. var_dump( ftell($file_handle) ); // expected : 0
  39. var_dump( fwrite($file_handle, $data_to_be_written, 400));
  40. var_dump( ftell($file_handle) ); // expected: 400
  41. var_dump( feof($file_handle) ); //Expecting bool(false)
  42. /*overwrite data in middle of the file*/
  43. fseek($file_handle, SEEK_SET, 1024/2 );
  44. var_dump( ftell($file_handle)); // expected: 1024/2
  45. var_dump( fwrite($file_handle, $data_to_be_written, 200) );
  46. var_dump( ftell($file_handle) );
  47. var_dump( feof($file_handle) ); //Expecting bool(false)
  48. /* write at the end of the file */
  49. fseek($file_handle, SEEK_END, 0);
  50. var_dump( ftell($file_handle) ); // expected: 1024
  51. var_dump( feof($file_handle) );
  52. var_dump( fwrite($file_handle, $data_to_be_written, 200) );
  53. var_dump( ftell($file_handle) );
  54. var_dump( feof($file_handle) ); //Expecting bool(false)
  55. /* display the file content, check the file size */
  56. var_dump( fclose($file_handle) );
  57. clearstatcache();//clears file status cache
  58. var_dump( filesize($filename) );
  59. var_dump(md5(file_get_contents($filename)));
  60. delete_file($filename); // delete file with name fwrite_variation2.tmp
  61. } // end of inner foreach loop
  62. } // end of outer foreach loop
  63. echo "Done\n";
  64. ?>
  65. --EXPECT--
  66. *** Testing fwrite() various operations ***
  67. -- Testing fwrite() with file having content of type numeric --
  68. -- Opening file in r+ --
  69. int(0)
  70. int(400)
  71. int(400)
  72. bool(false)
  73. int(400)
  74. int(200)
  75. int(600)
  76. bool(false)
  77. int(2)
  78. bool(false)
  79. int(200)
  80. int(202)
  81. bool(false)
  82. bool(true)
  83. int(1024)
  84. string(32) "950b7457d1deb6332f2fc5d42f3129d6"
  85. -- Opening file in r+b --
  86. int(0)
  87. int(400)
  88. int(400)
  89. bool(false)
  90. int(400)
  91. int(200)
  92. int(600)
  93. bool(false)
  94. int(2)
  95. bool(false)
  96. int(200)
  97. int(202)
  98. bool(false)
  99. bool(true)
  100. int(1024)
  101. string(32) "950b7457d1deb6332f2fc5d42f3129d6"
  102. -- Opening file in r+t --
  103. int(0)
  104. int(400)
  105. int(400)
  106. bool(false)
  107. int(400)
  108. int(200)
  109. int(600)
  110. bool(false)
  111. int(2)
  112. bool(false)
  113. int(200)
  114. int(202)
  115. bool(false)
  116. bool(true)
  117. int(1024)
  118. string(32) "950b7457d1deb6332f2fc5d42f3129d6"
  119. -- Testing fwrite() with file having content of type text --
  120. -- Opening file in r+ --
  121. int(0)
  122. int(400)
  123. int(400)
  124. bool(false)
  125. int(400)
  126. int(200)
  127. int(600)
  128. bool(false)
  129. int(2)
  130. bool(false)
  131. int(200)
  132. int(202)
  133. bool(false)
  134. bool(true)
  135. int(1024)
  136. string(32) "3bdaf80dae28bc24bb304daa5ffee16c"
  137. -- Opening file in r+b --
  138. int(0)
  139. int(400)
  140. int(400)
  141. bool(false)
  142. int(400)
  143. int(200)
  144. int(600)
  145. bool(false)
  146. int(2)
  147. bool(false)
  148. int(200)
  149. int(202)
  150. bool(false)
  151. bool(true)
  152. int(1024)
  153. string(32) "3bdaf80dae28bc24bb304daa5ffee16c"
  154. -- Opening file in r+t --
  155. int(0)
  156. int(400)
  157. int(400)
  158. bool(false)
  159. int(400)
  160. int(200)
  161. int(600)
  162. bool(false)
  163. int(2)
  164. bool(false)
  165. int(200)
  166. int(202)
  167. bool(false)
  168. bool(true)
  169. int(1024)
  170. string(32) "3bdaf80dae28bc24bb304daa5ffee16c"
  171. -- Testing fwrite() with file having content of type text_with_new_line --
  172. -- Opening file in r+ --
  173. int(0)
  174. int(400)
  175. int(400)
  176. bool(false)
  177. int(400)
  178. int(200)
  179. int(600)
  180. bool(false)
  181. int(2)
  182. bool(false)
  183. int(200)
  184. int(202)
  185. bool(false)
  186. bool(true)
  187. int(1024)
  188. string(32) "b188d7c8aa229cbef067e5970f2daba9"
  189. -- Opening file in r+b --
  190. int(0)
  191. int(400)
  192. int(400)
  193. bool(false)
  194. int(400)
  195. int(200)
  196. int(600)
  197. bool(false)
  198. int(2)
  199. bool(false)
  200. int(200)
  201. int(202)
  202. bool(false)
  203. bool(true)
  204. int(1024)
  205. string(32) "b188d7c8aa229cbef067e5970f2daba9"
  206. -- Opening file in r+t --
  207. int(0)
  208. int(400)
  209. int(400)
  210. bool(false)
  211. int(400)
  212. int(200)
  213. int(600)
  214. bool(false)
  215. int(2)
  216. bool(false)
  217. int(200)
  218. int(202)
  219. bool(false)
  220. bool(true)
  221. int(1024)
  222. string(32) "b188d7c8aa229cbef067e5970f2daba9"
  223. -- Testing fwrite() with file having content of type alphanumeric --
  224. -- Opening file in r+ --
  225. int(0)
  226. int(400)
  227. int(400)
  228. bool(false)
  229. int(400)
  230. int(200)
  231. int(600)
  232. bool(false)
  233. int(2)
  234. bool(false)
  235. int(200)
  236. int(202)
  237. bool(false)
  238. bool(true)
  239. int(1024)
  240. string(32) "5d4ec23a3d9dd447e2f702d9e0e114d9"
  241. -- Opening file in r+b --
  242. int(0)
  243. int(400)
  244. int(400)
  245. bool(false)
  246. int(400)
  247. int(200)
  248. int(600)
  249. bool(false)
  250. int(2)
  251. bool(false)
  252. int(200)
  253. int(202)
  254. bool(false)
  255. bool(true)
  256. int(1024)
  257. string(32) "5d4ec23a3d9dd447e2f702d9e0e114d9"
  258. -- Opening file in r+t --
  259. int(0)
  260. int(400)
  261. int(400)
  262. bool(false)
  263. int(400)
  264. int(200)
  265. int(600)
  266. bool(false)
  267. int(2)
  268. bool(false)
  269. int(200)
  270. int(202)
  271. bool(false)
  272. bool(true)
  273. int(1024)
  274. string(32) "5d4ec23a3d9dd447e2f702d9e0e114d9"
  275. Done