fwrite_variation1.phpt 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. --TEST--
  2. Test fwrite() function : usage variations - r, rb & rt modes
  3. --FILE--
  4. <?php
  5. echo "*** Testing fwrite() various operations ***\n";
  6. // include the file.inc for Function: function delete_file($filename)
  7. include ("file.inc");
  8. /*
  9. Test fwrite with file opened in mode : r,rb,rt
  10. File having content of type numeric, text,text_with_new_line & alphanumeric
  11. */
  12. $file_modes = array("r","rb","rt");
  13. $file_content_types = array("numeric","text","text_with_new_line","alphanumeric");
  14. foreach($file_content_types as $file_content_type) {
  15. echo "\n-- Testing fwrite() with file having content of type ". $file_content_type ." --\n";
  16. /* open the file using $files_modes and perform fwrite() on it */
  17. foreach($file_modes as $file_mode) {
  18. echo "-- Opening file in $file_mode --\n";
  19. // create the temp file with content of type $file_content_type
  20. $filename = __DIR__."/fwrite_variation1.tmp"; // this is name of the file
  21. create_files ( __DIR__, 1, $file_content_type, 0755, 1, "w", "fwrite_variation");
  22. $file_handle = fopen($filename, $file_mode);
  23. if(!$file_handle) {
  24. echo "Error: failed to fopen() file: $filename!";
  25. exit();
  26. }
  27. $data_to_be_written="";
  28. fill_buffer($data_to_be_written,$file_content_type,1024); //get the data of size 1024
  29. /* Write the data into the file, verify it by checking the file pointer position, eof position,
  30. filesize & by displaying the content */
  31. var_dump( ftell($file_handle) ); // expected: 0
  32. var_dump( fwrite($file_handle, $data_to_be_written ));
  33. var_dump( ftell($file_handle) ); // expected: 0
  34. var_dump( feof($file_handle) ); // expected: false
  35. // move the file pointer to end of the file and try fwrite()
  36. fseek($file_handle, SEEK_END, 0);
  37. var_dump( ftell($file_handle) ); // expecting 1024
  38. var_dump( fwrite($file_handle, $data_to_be_written) ); // fwrite to fail
  39. var_dump( ftell($file_handle) ); //check that file pointer points at eof, expected: 1024
  40. var_dump( feof($file_handle) ); // ensure that feof() points to eof, expected: true
  41. // ensure that file content/size didn't change.
  42. var_dump( fclose($file_handle) );
  43. clearstatcache();//clears file status cache
  44. var_dump( filesize($filename) ); // expected: 1024
  45. var_dump(md5(file_get_contents($filename))); // hash the output
  46. delete_file($filename); // delete file with name fwrite_variation1.tmp
  47. } // end of inner foreach loop
  48. } // end of outer foreach loop
  49. echo "Done\n";
  50. ?>
  51. --EXPECTF--
  52. *** Testing fwrite() various operations ***
  53. -- Testing fwrite() with file having content of type numeric --
  54. -- Opening file in r --
  55. int(0)
  56. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  57. bool(false)
  58. int(0)
  59. bool(false)
  60. int(2)
  61. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  62. bool(false)
  63. int(2)
  64. bool(false)
  65. bool(true)
  66. int(1024)
  67. string(32) "950b7457d1deb6332f2fc5d42f3129d6"
  68. -- Opening file in rb --
  69. int(0)
  70. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  71. bool(false)
  72. int(0)
  73. bool(false)
  74. int(2)
  75. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  76. bool(false)
  77. int(2)
  78. bool(false)
  79. bool(true)
  80. int(1024)
  81. string(32) "950b7457d1deb6332f2fc5d42f3129d6"
  82. -- Opening file in rt --
  83. int(0)
  84. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  85. bool(false)
  86. int(0)
  87. bool(false)
  88. int(2)
  89. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  90. bool(false)
  91. int(2)
  92. bool(false)
  93. bool(true)
  94. int(1024)
  95. string(32) "950b7457d1deb6332f2fc5d42f3129d6"
  96. -- Testing fwrite() with file having content of type text --
  97. -- Opening file in r --
  98. int(0)
  99. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  100. bool(false)
  101. int(0)
  102. bool(false)
  103. int(2)
  104. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  105. bool(false)
  106. int(2)
  107. bool(false)
  108. bool(true)
  109. int(1024)
  110. string(32) "e486000c4c8452774f746a27658d87fa"
  111. -- Opening file in rb --
  112. int(0)
  113. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  114. bool(false)
  115. int(0)
  116. bool(false)
  117. int(2)
  118. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  119. bool(false)
  120. int(2)
  121. bool(false)
  122. bool(true)
  123. int(1024)
  124. string(32) "e486000c4c8452774f746a27658d87fa"
  125. -- Opening file in rt --
  126. int(0)
  127. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  128. bool(false)
  129. int(0)
  130. bool(false)
  131. int(2)
  132. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  133. bool(false)
  134. int(2)
  135. bool(false)
  136. bool(true)
  137. int(1024)
  138. string(32) "e486000c4c8452774f746a27658d87fa"
  139. -- Testing fwrite() with file having content of type text_with_new_line --
  140. -- Opening file in r --
  141. int(0)
  142. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  143. bool(false)
  144. int(0)
  145. bool(false)
  146. int(2)
  147. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  148. bool(false)
  149. int(2)
  150. bool(false)
  151. bool(true)
  152. int(1024)
  153. string(32) "b09c8026a64a88d36d4c2f17983964bb"
  154. -- Opening file in rb --
  155. int(0)
  156. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  157. bool(false)
  158. int(0)
  159. bool(false)
  160. int(2)
  161. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  162. bool(false)
  163. int(2)
  164. bool(false)
  165. bool(true)
  166. int(1024)
  167. string(32) "b09c8026a64a88d36d4c2f17983964bb"
  168. -- Opening file in rt --
  169. int(0)
  170. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  171. bool(false)
  172. int(0)
  173. bool(false)
  174. int(2)
  175. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  176. bool(false)
  177. int(2)
  178. bool(false)
  179. bool(true)
  180. int(1024)
  181. string(32) "b09c8026a64a88d36d4c2f17983964bb"
  182. -- Testing fwrite() with file having content of type alphanumeric --
  183. -- Opening file in r --
  184. int(0)
  185. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  186. bool(false)
  187. int(0)
  188. bool(false)
  189. int(2)
  190. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  191. bool(false)
  192. int(2)
  193. bool(false)
  194. bool(true)
  195. int(1024)
  196. string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
  197. -- Opening file in rb --
  198. int(0)
  199. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  200. bool(false)
  201. int(0)
  202. bool(false)
  203. int(2)
  204. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  205. bool(false)
  206. int(2)
  207. bool(false)
  208. bool(true)
  209. int(1024)
  210. string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
  211. -- Opening file in rt --
  212. int(0)
  213. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  214. bool(false)
  215. int(0)
  216. bool(false)
  217. int(2)
  218. Notice: fwrite(): Write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d
  219. bool(false)
  220. int(2)
  221. bool(false)
  222. bool(true)
  223. int(1024)
  224. string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
  225. Done