fread_variation4-win32.phpt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. --TEST--
  2. Test fread() function : usage variations - read beyond file size, write only mode
  3. --SKIPIF--
  4. <?php
  5. if (substr(PHP_OS, 0, 3) != 'WIN') {
  6. die('skip...only valid for Windows');
  7. }
  8. ?>
  9. --FILE--
  10. <?php
  11. /*
  12. Prototype: string fread ( resource $handle [, int $length] );
  13. Description: reads up to length bytes from the file pointer referenced by handle.
  14. Reading stops when up to length bytes have been read, EOF (end of file) is
  15. reached, (for network streams) when a packet becomes available, or (after
  16. opening userspace stream) when 8192 bytes have been read whichever comes first.
  17. */
  18. // include the file.inc for common functions for test
  19. include ("file.inc");
  20. /* Function : function check_read(resource $file_handle, int $read_size, int $expect_size)
  21. Description : Read data from file of size $read_size and verifies that $expected_size no. of
  22. bytes are read.
  23. $file_handle : File Handle
  24. $read_size : No. of bytes to be read.
  25. $expect_size : Expected data length
  26. Returns: returns the data read
  27. */
  28. function check_read($file_handle, $read_size, $expect_size) {
  29. // print file pointer position before read
  30. var_dump( ftell($file_handle) );
  31. var_dump( feof($file_handle) );
  32. // read the data of size $read_size
  33. echo "Reading $read_size bytes from file, expecting $expect_size bytes ... ";
  34. $data_from_file = fread($file_handle, $read_size);
  35. // check if data read is of expected size
  36. if ( strlen($data_from_file) == $expect_size)
  37. echo "OK\n";
  38. else
  39. echo "Error reading file, total number of bytes read = ".strlen($data_from_file)."\n";
  40. // file pointer position after read
  41. var_dump( ftell($file_handle) );
  42. // check if file pointer at eof()
  43. var_dump( feof($file_handle) );
  44. return $data_from_file;
  45. }
  46. echo "*** Testing fread() : usage variations ***\n";
  47. $file_modes = array("a","ab","at",
  48. "w","wb","wt",
  49. "x","xb","xt" );
  50. $file_content_types = array("numeric","text","text_with_new_line");
  51. foreach($file_content_types as $file_content_type) {
  52. echo "\n-- Testing fread() with file having content of type ". $file_content_type ." --\n";
  53. /* open the file using $files_modes and perform fread() on it */
  54. foreach($file_modes as $file_mode) {
  55. if(!strstr($file_mode,"x")){
  56. /* create files with $file_content_type */
  57. create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 1, "w", "fread_variation", 4);
  58. }
  59. $filename = dirname(__FILE__)."/fread_variation4.tmp"; // this is name of the file created by create_files()
  60. echo "-- File opened in mode ".$file_mode." --\n";
  61. $file_handle = fopen($filename, $file_mode);
  62. if (!$file_handle) {
  63. echo "Error: failed to fopen() file: $filename!";
  64. exit();
  65. }
  66. if(strstr($file_mode,"w") || strstr($file_mode,"x") ) {
  67. fill_file($file_handle, $file_content_type, 1024);
  68. }
  69. rewind($file_handle);
  70. echo "-- Reading beyond filesize, expeceted : 1024 bytes --\n";
  71. // read file by giving size more than its size
  72. rewind($file_handle);
  73. $data_from_file = check_read($file_handle, 1030, ( strstr($file_mode, "+") ? 1024 : 0) );
  74. if ( $data_from_file != false)
  75. var_dump( md5($data_from_file) );
  76. echo "-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --\n";
  77. rewind($file_handle);
  78. // try fread when file pointer at end
  79. fseek($file_handle, 0, SEEK_END);
  80. //reading file when file pointer at end
  81. $data_from_file = check_read($file_handle, 10, 0);
  82. if ( $data_from_file != false)
  83. var_dump( md5($data_from_file) );
  84. // now close the file
  85. fclose($file_handle);
  86. // delete the file created
  87. delete_file($filename); // delete file
  88. } // end of inner foreach loop
  89. }// end of outer foreach loop
  90. echo"Done\n";
  91. ?>
  92. --EXPECTF--
  93. *** Testing fread() : usage variations ***
  94. -- Testing fread() with file having content of type numeric --
  95. -- File opened in mode a --
  96. -- Reading beyond filesize, expeceted : 1024 bytes --
  97. int(0)
  98. bool(false)
  99. Reading 1030 bytes from file, expecting 0 bytes ... OK
  100. int(0)
  101. bool(false)
  102. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  103. int(1024)
  104. bool(false)
  105. Reading 10 bytes from file, expecting 0 bytes ... OK
  106. int(1024)
  107. bool(false)
  108. -- File opened in mode ab --
  109. -- Reading beyond filesize, expeceted : 1024 bytes --
  110. int(0)
  111. bool(false)
  112. Reading 1030 bytes from file, expecting 0 bytes ... OK
  113. int(0)
  114. bool(false)
  115. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  116. int(1024)
  117. bool(false)
  118. Reading 10 bytes from file, expecting 0 bytes ... OK
  119. int(1024)
  120. bool(false)
  121. -- File opened in mode at --
  122. -- Reading beyond filesize, expeceted : 1024 bytes --
  123. int(0)
  124. bool(false)
  125. Reading 1030 bytes from file, expecting 0 bytes ... OK
  126. int(0)
  127. bool(false)
  128. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  129. int(1024)
  130. bool(false)
  131. Reading 10 bytes from file, expecting 0 bytes ... OK
  132. int(1024)
  133. bool(false)
  134. -- File opened in mode w --
  135. -- Reading beyond filesize, expeceted : 1024 bytes --
  136. int(0)
  137. bool(false)
  138. Reading 1030 bytes from file, expecting 0 bytes ... OK
  139. int(0)
  140. bool(false)
  141. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  142. int(1024)
  143. bool(false)
  144. Reading 10 bytes from file, expecting 0 bytes ... OK
  145. int(1024)
  146. bool(false)
  147. -- File opened in mode wb --
  148. -- Reading beyond filesize, expeceted : 1024 bytes --
  149. int(0)
  150. bool(false)
  151. Reading 1030 bytes from file, expecting 0 bytes ... OK
  152. int(0)
  153. bool(false)
  154. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  155. int(1024)
  156. bool(false)
  157. Reading 10 bytes from file, expecting 0 bytes ... OK
  158. int(1024)
  159. bool(false)
  160. -- File opened in mode wt --
  161. -- Reading beyond filesize, expeceted : 1024 bytes --
  162. int(0)
  163. bool(false)
  164. Reading 1030 bytes from file, expecting 0 bytes ... OK
  165. int(0)
  166. bool(false)
  167. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  168. int(1024)
  169. bool(false)
  170. Reading 10 bytes from file, expecting 0 bytes ... OK
  171. int(1024)
  172. bool(false)
  173. -- File opened in mode x --
  174. -- Reading beyond filesize, expeceted : 1024 bytes --
  175. int(0)
  176. bool(false)
  177. Reading 1030 bytes from file, expecting 0 bytes ... OK
  178. int(0)
  179. bool(false)
  180. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  181. int(1024)
  182. bool(false)
  183. Reading 10 bytes from file, expecting 0 bytes ... OK
  184. int(1024)
  185. bool(false)
  186. -- File opened in mode xb --
  187. -- Reading beyond filesize, expeceted : 1024 bytes --
  188. int(0)
  189. bool(false)
  190. Reading 1030 bytes from file, expecting 0 bytes ... OK
  191. int(0)
  192. bool(false)
  193. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  194. int(1024)
  195. bool(false)
  196. Reading 10 bytes from file, expecting 0 bytes ... OK
  197. int(1024)
  198. bool(false)
  199. -- File opened in mode xt --
  200. -- Reading beyond filesize, expeceted : 1024 bytes --
  201. int(0)
  202. bool(false)
  203. Reading 1030 bytes from file, expecting 0 bytes ... OK
  204. int(0)
  205. bool(false)
  206. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  207. int(1024)
  208. bool(false)
  209. Reading 10 bytes from file, expecting 0 bytes ... OK
  210. int(1024)
  211. bool(false)
  212. -- Testing fread() with file having content of type text --
  213. -- File opened in mode a --
  214. -- Reading beyond filesize, expeceted : 1024 bytes --
  215. int(0)
  216. bool(false)
  217. Reading 1030 bytes from file, expecting 0 bytes ... OK
  218. int(0)
  219. bool(false)
  220. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  221. int(1024)
  222. bool(false)
  223. Reading 10 bytes from file, expecting 0 bytes ... OK
  224. int(1024)
  225. bool(false)
  226. -- File opened in mode ab --
  227. -- Reading beyond filesize, expeceted : 1024 bytes --
  228. int(0)
  229. bool(false)
  230. Reading 1030 bytes from file, expecting 0 bytes ... OK
  231. int(0)
  232. bool(false)
  233. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  234. int(1024)
  235. bool(false)
  236. Reading 10 bytes from file, expecting 0 bytes ... OK
  237. int(1024)
  238. bool(false)
  239. -- File opened in mode at --
  240. -- Reading beyond filesize, expeceted : 1024 bytes --
  241. int(0)
  242. bool(false)
  243. Reading 1030 bytes from file, expecting 0 bytes ... OK
  244. int(0)
  245. bool(false)
  246. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  247. int(1024)
  248. bool(false)
  249. Reading 10 bytes from file, expecting 0 bytes ... OK
  250. int(1024)
  251. bool(false)
  252. -- File opened in mode w --
  253. -- Reading beyond filesize, expeceted : 1024 bytes --
  254. int(0)
  255. bool(false)
  256. Reading 1030 bytes from file, expecting 0 bytes ... OK
  257. int(0)
  258. bool(false)
  259. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  260. int(1024)
  261. bool(false)
  262. Reading 10 bytes from file, expecting 0 bytes ... OK
  263. int(1024)
  264. bool(false)
  265. -- File opened in mode wb --
  266. -- Reading beyond filesize, expeceted : 1024 bytes --
  267. int(0)
  268. bool(false)
  269. Reading 1030 bytes from file, expecting 0 bytes ... OK
  270. int(0)
  271. bool(false)
  272. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  273. int(1024)
  274. bool(false)
  275. Reading 10 bytes from file, expecting 0 bytes ... OK
  276. int(1024)
  277. bool(false)
  278. -- File opened in mode wt --
  279. -- Reading beyond filesize, expeceted : 1024 bytes --
  280. int(0)
  281. bool(false)
  282. Reading 1030 bytes from file, expecting 0 bytes ... OK
  283. int(0)
  284. bool(false)
  285. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  286. int(1024)
  287. bool(false)
  288. Reading 10 bytes from file, expecting 0 bytes ... OK
  289. int(1024)
  290. bool(false)
  291. -- File opened in mode x --
  292. -- Reading beyond filesize, expeceted : 1024 bytes --
  293. int(0)
  294. bool(false)
  295. Reading 1030 bytes from file, expecting 0 bytes ... OK
  296. int(0)
  297. bool(false)
  298. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  299. int(1024)
  300. bool(false)
  301. Reading 10 bytes from file, expecting 0 bytes ... OK
  302. int(1024)
  303. bool(false)
  304. -- File opened in mode xb --
  305. -- Reading beyond filesize, expeceted : 1024 bytes --
  306. int(0)
  307. bool(false)
  308. Reading 1030 bytes from file, expecting 0 bytes ... OK
  309. int(0)
  310. bool(false)
  311. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  312. int(1024)
  313. bool(false)
  314. Reading 10 bytes from file, expecting 0 bytes ... OK
  315. int(1024)
  316. bool(false)
  317. -- File opened in mode xt --
  318. -- Reading beyond filesize, expeceted : 1024 bytes --
  319. int(0)
  320. bool(false)
  321. Reading 1030 bytes from file, expecting 0 bytes ... OK
  322. int(0)
  323. bool(false)
  324. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  325. int(1024)
  326. bool(false)
  327. Reading 10 bytes from file, expecting 0 bytes ... OK
  328. int(1024)
  329. bool(false)
  330. -- Testing fread() with file having content of type text_with_new_line --
  331. -- File opened in mode a --
  332. -- Reading beyond filesize, expeceted : 1024 bytes --
  333. int(0)
  334. bool(false)
  335. Reading 1030 bytes from file, expecting 0 bytes ... OK
  336. int(0)
  337. bool(false)
  338. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  339. int(1024)
  340. bool(false)
  341. Reading 10 bytes from file, expecting 0 bytes ... OK
  342. int(1024)
  343. bool(false)
  344. -- File opened in mode ab --
  345. -- Reading beyond filesize, expeceted : 1024 bytes --
  346. int(0)
  347. bool(false)
  348. Reading 1030 bytes from file, expecting 0 bytes ... OK
  349. int(0)
  350. bool(false)
  351. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  352. int(1024)
  353. bool(false)
  354. Reading 10 bytes from file, expecting 0 bytes ... OK
  355. int(1024)
  356. bool(false)
  357. -- File opened in mode at --
  358. -- Reading beyond filesize, expeceted : 1024 bytes --
  359. int(0)
  360. bool(false)
  361. Reading 1030 bytes from file, expecting 0 bytes ... OK
  362. int(0)
  363. bool(false)
  364. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  365. int(1024)
  366. bool(false)
  367. Reading 10 bytes from file, expecting 0 bytes ... OK
  368. int(1024)
  369. bool(false)
  370. -- File opened in mode w --
  371. -- Reading beyond filesize, expeceted : 1024 bytes --
  372. int(0)
  373. bool(false)
  374. Reading 1030 bytes from file, expecting 0 bytes ... OK
  375. int(0)
  376. bool(false)
  377. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  378. int(1024)
  379. bool(false)
  380. Reading 10 bytes from file, expecting 0 bytes ... OK
  381. int(1024)
  382. bool(false)
  383. -- File opened in mode wb --
  384. -- Reading beyond filesize, expeceted : 1024 bytes --
  385. int(0)
  386. bool(false)
  387. Reading 1030 bytes from file, expecting 0 bytes ... OK
  388. int(0)
  389. bool(false)
  390. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  391. int(1024)
  392. bool(false)
  393. Reading 10 bytes from file, expecting 0 bytes ... OK
  394. int(1024)
  395. bool(false)
  396. -- File opened in mode wt --
  397. -- Reading beyond filesize, expeceted : 1024 bytes --
  398. int(0)
  399. bool(false)
  400. Reading 1030 bytes from file, expecting 0 bytes ... OK
  401. int(0)
  402. bool(false)
  403. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  404. int(1137)
  405. bool(false)
  406. Reading 10 bytes from file, expecting 0 bytes ... OK
  407. int(1137)
  408. bool(false)
  409. -- File opened in mode x --
  410. -- Reading beyond filesize, expeceted : 1024 bytes --
  411. int(0)
  412. bool(false)
  413. Reading 1030 bytes from file, expecting 0 bytes ... OK
  414. int(0)
  415. bool(false)
  416. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  417. int(1024)
  418. bool(false)
  419. Reading 10 bytes from file, expecting 0 bytes ... OK
  420. int(1024)
  421. bool(false)
  422. -- File opened in mode xb --
  423. -- Reading beyond filesize, expeceted : 1024 bytes --
  424. int(0)
  425. bool(false)
  426. Reading 1030 bytes from file, expecting 0 bytes ... OK
  427. int(0)
  428. bool(false)
  429. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  430. int(1024)
  431. bool(false)
  432. Reading 10 bytes from file, expecting 0 bytes ... OK
  433. int(1024)
  434. bool(false)
  435. -- File opened in mode xt --
  436. -- Reading beyond filesize, expeceted : 1024 bytes --
  437. int(0)
  438. bool(false)
  439. Reading 1030 bytes from file, expecting 0 bytes ... OK
  440. int(0)
  441. bool(false)
  442. -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
  443. int(1137)
  444. bool(false)
  445. Reading 10 bytes from file, expecting 0 bytes ... OK
  446. int(1137)
  447. bool(false)
  448. Done