fread_variation2.phpt 16 KB

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