fwrite_variation1.phpt 5.2 KB

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