fwrite_basic-win32.phpt 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. --TEST--
  2. Test fwrite() function : basic functionality
  3. --SKIPIF--
  4. <?php
  5. if( substr(PHP_OS, 0, 3) != 'WIN' ) {
  6. die('skip...Valid for Windows only');
  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. // include the file.inc for Function: function delete_file($filename)
  19. include ("file.inc");
  20. echo "*** Testing fwrite() basic operations ***\n";
  21. /*
  22. test fwrite with file opened in mode : w,wb,wt,w+,w+b,w+t
  23. File containing data of type, numeric, text, text_with_new_line, alphanumeric
  24. */
  25. $file_modes = array( "w", "wb", "wt", "w+", "w+b", "w+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 data of type ". $file_content_type ." --\n";
  29. $filename = dirname(__FILE__)."/fwrite_basic-win32.tmp"; // this is name of the file
  30. for($inner_loop_counter = 0;
  31. $inner_loop_counter < count($file_modes);
  32. $inner_loop_counter++) {
  33. echo "-- File opened in mode : " . $file_modes[$inner_loop_counter]. " --\n";
  34. /* open the file using $files_modes and perform fwrite() on it */
  35. $file_handle = fopen($filename, $file_modes[$inner_loop_counter]);
  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 in to the file, verify the write by checking file pointer position,
  43. eof position, and data. */
  44. // writing 100 bytes
  45. var_dump( ftell($file_handle) ); // Expecting 0
  46. var_dump( fwrite($file_handle, $data_to_be_written, 100)); //int(100)
  47. var_dump( feof($file_handle) ); // expected : false
  48. var_dump( ftell($file_handle) ); //expected: 100
  49. // trying to write more than the available data, available 1024 bytes but trying 2048
  50. var_dump( fwrite($file_handle, $data_to_be_written, 2048)); //int(1024)
  51. var_dump( feof($file_handle) ); // expected : false
  52. var_dump( ftell($file_handle) ); // expected: 1124
  53. // fwrite() without length parameter
  54. var_dump( fwrite($file_handle, $data_to_be_written)); //int(1024)
  55. var_dump( ftell($file_handle) ); // expected: 2148
  56. var_dump( feof($file_handle) ); // expected: false
  57. // close the file, get the size and content of the file.
  58. var_dump( fclose($file_handle) ); //expected : true
  59. clearstatcache();//clears file status cache
  60. var_dump( filesize($filename) ); // expected: 2148
  61. var_dump(md5(file_get_contents($filename))); // hash the output
  62. } // end of inner for loop
  63. // delete the file created : fwrite_basic.tmp
  64. delete_file($filename);
  65. } // end of outer foreach loop
  66. echo "Done\n";
  67. ?>
  68. --EXPECTF--
  69. *** Testing fwrite() basic operations ***
  70. -- Testing fwrite() with file having data of type numeric --
  71. -- File opened in mode : w --
  72. int(0)
  73. int(100)
  74. bool(false)
  75. int(100)
  76. int(1024)
  77. bool(false)
  78. int(1124)
  79. int(1024)
  80. int(2148)
  81. bool(false)
  82. bool(true)
  83. int(2148)
  84. string(32) "04db34906fe2c56dcfbd649b7d916974"
  85. -- File opened in mode : wb --
  86. int(0)
  87. int(100)
  88. bool(false)
  89. int(100)
  90. int(1024)
  91. bool(false)
  92. int(1124)
  93. int(1024)
  94. int(2148)
  95. bool(false)
  96. bool(true)
  97. int(2148)
  98. string(32) "04db34906fe2c56dcfbd649b7d916974"
  99. -- File opened in mode : wt --
  100. int(0)
  101. int(100)
  102. bool(false)
  103. int(100)
  104. int(1024)
  105. bool(false)
  106. int(1124)
  107. int(1024)
  108. int(2148)
  109. bool(false)
  110. bool(true)
  111. int(2148)
  112. string(32) "04db34906fe2c56dcfbd649b7d916974"
  113. -- File opened in mode : w+ --
  114. int(0)
  115. int(100)
  116. bool(false)
  117. int(100)
  118. int(1024)
  119. bool(false)
  120. int(1124)
  121. int(1024)
  122. int(2148)
  123. bool(false)
  124. bool(true)
  125. int(2148)
  126. string(32) "04db34906fe2c56dcfbd649b7d916974"
  127. -- File opened in mode : w+b --
  128. int(0)
  129. int(100)
  130. bool(false)
  131. int(100)
  132. int(1024)
  133. bool(false)
  134. int(1124)
  135. int(1024)
  136. int(2148)
  137. bool(false)
  138. bool(true)
  139. int(2148)
  140. string(32) "04db34906fe2c56dcfbd649b7d916974"
  141. -- File opened in mode : w+t --
  142. int(0)
  143. int(100)
  144. bool(false)
  145. int(100)
  146. int(1024)
  147. bool(false)
  148. int(1124)
  149. int(1024)
  150. int(2148)
  151. bool(false)
  152. bool(true)
  153. int(2148)
  154. string(32) "04db34906fe2c56dcfbd649b7d916974"
  155. -- Testing fwrite() with file having data of type text --
  156. -- File opened in mode : w --
  157. int(0)
  158. int(100)
  159. bool(false)
  160. int(100)
  161. int(1024)
  162. bool(false)
  163. int(1124)
  164. int(1024)
  165. int(2148)
  166. bool(false)
  167. bool(true)
  168. int(2148)
  169. string(32) "9c08ac77b7a93a84dd0b055900165e84"
  170. -- File opened in mode : wb --
  171. int(0)
  172. int(100)
  173. bool(false)
  174. int(100)
  175. int(1024)
  176. bool(false)
  177. int(1124)
  178. int(1024)
  179. int(2148)
  180. bool(false)
  181. bool(true)
  182. int(2148)
  183. string(32) "9c08ac77b7a93a84dd0b055900165e84"
  184. -- File opened in mode : wt --
  185. int(0)
  186. int(100)
  187. bool(false)
  188. int(100)
  189. int(1024)
  190. bool(false)
  191. int(1124)
  192. int(1024)
  193. int(2148)
  194. bool(false)
  195. bool(true)
  196. int(2148)
  197. string(32) "9c08ac77b7a93a84dd0b055900165e84"
  198. -- File opened in mode : w+ --
  199. int(0)
  200. int(100)
  201. bool(false)
  202. int(100)
  203. int(1024)
  204. bool(false)
  205. int(1124)
  206. int(1024)
  207. int(2148)
  208. bool(false)
  209. bool(true)
  210. int(2148)
  211. string(32) "9c08ac77b7a93a84dd0b055900165e84"
  212. -- File opened in mode : w+b --
  213. int(0)
  214. int(100)
  215. bool(false)
  216. int(100)
  217. int(1024)
  218. bool(false)
  219. int(1124)
  220. int(1024)
  221. int(2148)
  222. bool(false)
  223. bool(true)
  224. int(2148)
  225. string(32) "9c08ac77b7a93a84dd0b055900165e84"
  226. -- File opened in mode : w+t --
  227. int(0)
  228. int(100)
  229. bool(false)
  230. int(100)
  231. int(1024)
  232. bool(false)
  233. int(1124)
  234. int(1024)
  235. int(2148)
  236. bool(false)
  237. bool(true)
  238. int(2148)
  239. string(32) "9c08ac77b7a93a84dd0b055900165e84"
  240. -- Testing fwrite() with file having data of type text_with_new_line --
  241. -- File opened in mode : w --
  242. int(0)
  243. int(100)
  244. bool(false)
  245. int(100)
  246. int(1024)
  247. bool(false)
  248. int(1124)
  249. int(1024)
  250. int(2148)
  251. bool(false)
  252. bool(true)
  253. int(2148)
  254. string(32) "56a1963cc292d7f8245219116d9eca40"
  255. -- File opened in mode : wb --
  256. int(0)
  257. int(100)
  258. bool(false)
  259. int(100)
  260. int(1024)
  261. bool(false)
  262. int(1124)
  263. int(1024)
  264. int(2148)
  265. bool(false)
  266. bool(true)
  267. int(2148)
  268. string(32) "56a1963cc292d7f8245219116d9eca40"
  269. -- File opened in mode : wt --
  270. int(0)
  271. int(100)
  272. bool(false)
  273. int(100)
  274. int(1024)
  275. bool(false)
  276. int(1124)
  277. int(1024)
  278. int(2148)
  279. bool(false)
  280. bool(true)
  281. int(2385)
  282. string(32) "62b09dac6d598bf54de7b02e0e68e5c7"
  283. -- File opened in mode : w+ --
  284. int(0)
  285. int(100)
  286. bool(false)
  287. int(100)
  288. int(1024)
  289. bool(false)
  290. int(1124)
  291. int(1024)
  292. int(2148)
  293. bool(false)
  294. bool(true)
  295. int(2148)
  296. string(32) "56a1963cc292d7f8245219116d9eca40"
  297. -- File opened in mode : w+b --
  298. int(0)
  299. int(100)
  300. bool(false)
  301. int(100)
  302. int(1024)
  303. bool(false)
  304. int(1124)
  305. int(1024)
  306. int(2148)
  307. bool(false)
  308. bool(true)
  309. int(2148)
  310. string(32) "56a1963cc292d7f8245219116d9eca40"
  311. -- File opened in mode : w+t --
  312. int(0)
  313. int(100)
  314. bool(false)
  315. int(100)
  316. int(1024)
  317. bool(false)
  318. int(1124)
  319. int(1024)
  320. int(2148)
  321. bool(false)
  322. bool(true)
  323. int(2385)
  324. string(32) "62b09dac6d598bf54de7b02e0e68e5c7"
  325. -- Testing fwrite() with file having data of type alphanumeric --
  326. -- File opened in mode : w --
  327. int(0)
  328. int(100)
  329. bool(false)
  330. int(100)
  331. int(1024)
  332. bool(false)
  333. int(1124)
  334. int(1024)
  335. int(2148)
  336. bool(false)
  337. bool(true)
  338. int(2148)
  339. string(32) "719e3329c19218c12d232f2ee81e100f"
  340. -- File opened in mode : wb --
  341. int(0)
  342. int(100)
  343. bool(false)
  344. int(100)
  345. int(1024)
  346. bool(false)
  347. int(1124)
  348. int(1024)
  349. int(2148)
  350. bool(false)
  351. bool(true)
  352. int(2148)
  353. string(32) "719e3329c19218c12d232f2ee81e100f"
  354. -- File opened in mode : wt --
  355. int(0)
  356. int(100)
  357. bool(false)
  358. int(100)
  359. int(1024)
  360. bool(false)
  361. int(1124)
  362. int(1024)
  363. int(2148)
  364. bool(false)
  365. bool(true)
  366. int(2148)
  367. string(32) "719e3329c19218c12d232f2ee81e100f"
  368. -- File opened in mode : w+ --
  369. int(0)
  370. int(100)
  371. bool(false)
  372. int(100)
  373. int(1024)
  374. bool(false)
  375. int(1124)
  376. int(1024)
  377. int(2148)
  378. bool(false)
  379. bool(true)
  380. int(2148)
  381. string(32) "719e3329c19218c12d232f2ee81e100f"
  382. -- File opened in mode : w+b --
  383. int(0)
  384. int(100)
  385. bool(false)
  386. int(100)
  387. int(1024)
  388. bool(false)
  389. int(1124)
  390. int(1024)
  391. int(2148)
  392. bool(false)
  393. bool(true)
  394. int(2148)
  395. string(32) "719e3329c19218c12d232f2ee81e100f"
  396. -- File opened in mode : w+t --
  397. int(0)
  398. int(100)
  399. bool(false)
  400. int(100)
  401. int(1024)
  402. bool(false)
  403. int(1124)
  404. int(1024)
  405. int(2148)
  406. bool(false)
  407. bool(true)
  408. int(2148)
  409. string(32) "719e3329c19218c12d232f2ee81e100f"
  410. Done