fwrite_variation3.phpt 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. --TEST--
  2. Test fwrite() function : usage variations - a, ab, at, a+, a+b & a+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 : a,ab,at,a+,a+b,a+t
  23. File having content of type numeric, text,text_with_new_line & alphanumeric
  24. */
  25. $file_modes = array("a","ab","at","a+","a+b","a+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. // create temp file and fill it content of type $file_content_type
  33. $filename = dirname(__FILE__)."/fwrite_variation3.tmp"; // this is name of the file
  34. create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 1, "w", "fwrite_variation", 3);
  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. // append the data to the file, starting from current position of the file pointer
  45. var_dump( ftell($file_handle) ); // expected: 1024
  46. var_dump( fwrite($file_handle,$data_to_be_written,400) );
  47. var_dump( ftell($file_handle) ); // expected: 1024 + 400
  48. var_dump( feof($file_handle) ); // expected : true
  49. /*overwrite data in middle of the file*/
  50. fseek($file_handle, SEEK_SET, (1024 + 400)/2 );
  51. var_dump( ftell($file_handle)); // expected: (1024 + 400)/2
  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. /* check the filesize and display file content */
  56. // close the file, get the size and content of the file.
  57. var_dump( fclose($file_handle) );
  58. clearstatcache();//clears file status cache
  59. var_dump( filesize($filename) );
  60. var_dump(md5(file_get_contents($filename)));
  61. // delete the file created
  62. delete_file($filename); // delete file with name fwrite_variation3.tmp
  63. } // end of inner foreach loop
  64. } // end of outer foreach loop
  65. echo "Done\n";
  66. ?>
  67. --EXPECTF--
  68. *** Testing fwrite() various operations ***
  69. -- Testing fwrite() with file having content of type numeric --
  70. -- Opening file in a --
  71. int(0)
  72. int(400)
  73. int(400)
  74. bool(false)
  75. int(400)
  76. int(200)
  77. int(600)
  78. bool(false)
  79. bool(true)
  80. int(1624)
  81. string(32) "59ce5bf03b69069d00d6354bdc969ff6"
  82. -- Opening file in ab --
  83. int(0)
  84. int(400)
  85. int(400)
  86. bool(false)
  87. int(400)
  88. int(200)
  89. int(600)
  90. bool(false)
  91. bool(true)
  92. int(1624)
  93. string(32) "59ce5bf03b69069d00d6354bdc969ff6"
  94. -- Opening file in at --
  95. int(0)
  96. int(400)
  97. int(400)
  98. bool(false)
  99. int(400)
  100. int(200)
  101. int(600)
  102. bool(false)
  103. bool(true)
  104. int(1624)
  105. string(32) "59ce5bf03b69069d00d6354bdc969ff6"
  106. -- Opening file in a+ --
  107. int(0)
  108. int(400)
  109. int(400)
  110. bool(false)
  111. int(400)
  112. int(200)
  113. int(600)
  114. bool(false)
  115. bool(true)
  116. int(1624)
  117. string(32) "59ce5bf03b69069d00d6354bdc969ff6"
  118. -- Opening file in a+b --
  119. int(0)
  120. int(400)
  121. int(400)
  122. bool(false)
  123. int(400)
  124. int(200)
  125. int(600)
  126. bool(false)
  127. bool(true)
  128. int(1624)
  129. string(32) "59ce5bf03b69069d00d6354bdc969ff6"
  130. -- Opening file in a+t --
  131. int(0)
  132. int(400)
  133. int(400)
  134. bool(false)
  135. int(400)
  136. int(200)
  137. int(600)
  138. bool(false)
  139. bool(true)
  140. int(1624)
  141. string(32) "59ce5bf03b69069d00d6354bdc969ff6"
  142. -- Testing fwrite() with file having content of type text --
  143. -- Opening file in a --
  144. int(0)
  145. int(400)
  146. int(400)
  147. bool(false)
  148. int(400)
  149. int(200)
  150. int(600)
  151. bool(false)
  152. bool(true)
  153. int(1624)
  154. string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
  155. -- Opening file in ab --
  156. int(0)
  157. int(400)
  158. int(400)
  159. bool(false)
  160. int(400)
  161. int(200)
  162. int(600)
  163. bool(false)
  164. bool(true)
  165. int(1624)
  166. string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
  167. -- Opening file in at --
  168. int(0)
  169. int(400)
  170. int(400)
  171. bool(false)
  172. int(400)
  173. int(200)
  174. int(600)
  175. bool(false)
  176. bool(true)
  177. int(1624)
  178. string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
  179. -- Opening file in a+ --
  180. int(0)
  181. int(400)
  182. int(400)
  183. bool(false)
  184. int(400)
  185. int(200)
  186. int(600)
  187. bool(false)
  188. bool(true)
  189. int(1624)
  190. string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
  191. -- Opening file in a+b --
  192. int(0)
  193. int(400)
  194. int(400)
  195. bool(false)
  196. int(400)
  197. int(200)
  198. int(600)
  199. bool(false)
  200. bool(true)
  201. int(1624)
  202. string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
  203. -- Opening file in a+t --
  204. int(0)
  205. int(400)
  206. int(400)
  207. bool(false)
  208. int(400)
  209. int(200)
  210. int(600)
  211. bool(false)
  212. bool(true)
  213. int(1624)
  214. string(32) "dbd9dffd809d82e299bc1e5c55087f3b"
  215. -- Testing fwrite() with file having content of type text_with_new_line --
  216. -- Opening file in a --
  217. int(0)
  218. int(400)
  219. int(400)
  220. bool(false)
  221. int(400)
  222. int(200)
  223. int(600)
  224. bool(false)
  225. bool(true)
  226. int(1624)
  227. string(32) "3f0a483fe8a2f405677844e0b1af6cf4"
  228. -- Opening file in ab --
  229. int(0)
  230. int(400)
  231. int(400)
  232. bool(false)
  233. int(400)
  234. int(200)
  235. int(600)
  236. bool(false)
  237. bool(true)
  238. int(1624)
  239. string(32) "3f0a483fe8a2f405677844e0b1af6cf4"
  240. -- Opening file in at --
  241. int(0)
  242. int(400)
  243. int(400)
  244. bool(false)
  245. int(400)
  246. int(200)
  247. int(600)
  248. bool(false)
  249. bool(true)
  250. int(1624)
  251. string(32) "3f0a483fe8a2f405677844e0b1af6cf4"
  252. -- Opening file in a+ --
  253. int(0)
  254. int(400)
  255. int(400)
  256. bool(false)
  257. int(400)
  258. int(200)
  259. int(600)
  260. bool(false)
  261. bool(true)
  262. int(1624)
  263. string(32) "3f0a483fe8a2f405677844e0b1af6cf4"
  264. -- Opening file in a+b --
  265. int(0)
  266. int(400)
  267. int(400)
  268. bool(false)
  269. int(400)
  270. int(200)
  271. int(600)
  272. bool(false)
  273. bool(true)
  274. int(1624)
  275. string(32) "3f0a483fe8a2f405677844e0b1af6cf4"
  276. -- Opening file in a+t --
  277. int(0)
  278. int(400)
  279. int(400)
  280. bool(false)
  281. int(400)
  282. int(200)
  283. int(600)
  284. bool(false)
  285. bool(true)
  286. int(1624)
  287. string(32) "3f0a483fe8a2f405677844e0b1af6cf4"
  288. -- Testing fwrite() with file having content of type alphanumeric --
  289. -- Opening file in a --
  290. int(0)
  291. int(400)
  292. int(400)
  293. bool(false)
  294. int(400)
  295. int(200)
  296. int(600)
  297. bool(false)
  298. bool(true)
  299. int(1624)
  300. string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
  301. -- Opening file in ab --
  302. int(0)
  303. int(400)
  304. int(400)
  305. bool(false)
  306. int(400)
  307. int(200)
  308. int(600)
  309. bool(false)
  310. bool(true)
  311. int(1624)
  312. string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
  313. -- Opening file in at --
  314. int(0)
  315. int(400)
  316. int(400)
  317. bool(false)
  318. int(400)
  319. int(200)
  320. int(600)
  321. bool(false)
  322. bool(true)
  323. int(1624)
  324. string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
  325. -- Opening file in a+ --
  326. int(0)
  327. int(400)
  328. int(400)
  329. bool(false)
  330. int(400)
  331. int(200)
  332. int(600)
  333. bool(false)
  334. bool(true)
  335. int(1624)
  336. string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
  337. -- Opening file in a+b --
  338. int(0)
  339. int(400)
  340. int(400)
  341. bool(false)
  342. int(400)
  343. int(200)
  344. int(600)
  345. bool(false)
  346. bool(true)
  347. int(1624)
  348. string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
  349. -- Opening file in a+t --
  350. int(0)
  351. int(400)
  352. int(400)
  353. bool(false)
  354. int(400)
  355. int(200)
  356. int(600)
  357. bool(false)
  358. bool(true)
  359. int(1624)
  360. string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9"
  361. Done