ftruncate_variation6.phpt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. --TEST--
  2. Test ftruncate() function : usage variations - truncate file to smaller size
  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: bool ftruncate ( resource $handle, int $size );
  13. Description: Truncates a file to a given length
  14. */
  15. /* truncate the file to smaller size and display the content */
  16. // include common file related test functions
  17. include ("file.inc");
  18. echo "*** Testing ftruncate() : usage variations ***\n";
  19. /* test ftruncate with file opened in different modes */
  20. $file_modes = array("r", "rb", "rt", "r+", "r+b", "r+t",
  21. "w", "wb", "wt", "w+", "w+b", "w+t",
  22. "x", "xb", "xt", "x+", "x+b", "x+t",
  23. "a", "ab", "at", "a+", "a+b", "a+t");
  24. $file_content_types = array("numeric","text_with_new_line");
  25. foreach($file_content_types as $file_content_type) {
  26. echo "\n-- Testing ftruncate() with file having data of type ". $file_content_type ." --\n";
  27. for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
  28. echo "-- Testing ftruncate() with file opening using $file_modes[$mode_counter] mode --\n";
  29. // create 1 file with some contents
  30. $filename = dirname(__FILE__)."/ftruncate_variation6.tmp";
  31. if( strstr($file_modes[$mode_counter], "x") || strstr($file_modes[$mode_counter], "w") ) {
  32. // fopen the file using the $file_modes
  33. $file_handle = fopen($filename, $file_modes[$mode_counter]);
  34. fill_file($file_handle, $file_content_type, 1024);
  35. } else {
  36. create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 1, "w", "ftruncate_variation", 6);
  37. // fopen the file using the $file_modes
  38. $file_handle = fopen($filename, $file_modes[$mode_counter]);
  39. }
  40. if (!$file_handle) {
  41. echo "Error: failed to open file $filename!\n";
  42. exit();
  43. }
  44. rewind($file_handle); // file pointer to 0
  45. echo "-- Testing ftruncate(): truncate to smaller size and display the file content --\n";
  46. /* try to truncate it and display the file content */
  47. $new_size = 15;
  48. var_dump( filesize($filename) ); // current filesize
  49. var_dump( ftell($file_handle) );
  50. if(ftruncate($file_handle, $new_size) ){// truncate it
  51. echo "File content after truncating file to $new_size size : ";
  52. var_dump( file_get_contents($filename) );
  53. }
  54. var_dump( ftell($file_handle) );
  55. var_dump( feof($file_handle) );
  56. fclose($file_handle);
  57. clearstatcache(); // clear previous size value in cache
  58. var_dump( filesize($filename) );
  59. //delete all files created
  60. delete_file( $filename );
  61. }//end of inner for loop
  62. }//end of outer foreach loop
  63. echo "Done\n";
  64. ?>
  65. --EXPECTF--
  66. *** Testing ftruncate() : usage variations ***
  67. -- Testing ftruncate() with file having data of type numeric --
  68. -- Testing ftruncate() with file opening using r mode --
  69. -- Testing ftruncate(): truncate to smaller size and display the file content --
  70. int(1024)
  71. int(0)
  72. int(0)
  73. bool(false)
  74. int(1024)
  75. -- Testing ftruncate() with file opening using rb mode --
  76. -- Testing ftruncate(): truncate to smaller size and display the file content --
  77. int(1024)
  78. int(0)
  79. int(0)
  80. bool(false)
  81. int(1024)
  82. -- Testing ftruncate() with file opening using rt mode --
  83. -- Testing ftruncate(): truncate to smaller size and display the file content --
  84. int(1024)
  85. int(0)
  86. int(0)
  87. bool(false)
  88. int(1024)
  89. -- Testing ftruncate() with file opening using r+ mode --
  90. -- Testing ftruncate(): truncate to smaller size and display the file content --
  91. int(1024)
  92. int(0)
  93. File content after truncating file to 15 size : string(15) "222222222222222"
  94. int(0)
  95. bool(false)
  96. int(15)
  97. -- Testing ftruncate() with file opening using r+b mode --
  98. -- Testing ftruncate(): truncate to smaller size and display the file content --
  99. int(1024)
  100. int(0)
  101. File content after truncating file to 15 size : string(15) "222222222222222"
  102. int(0)
  103. bool(false)
  104. int(15)
  105. -- Testing ftruncate() with file opening using r+t mode --
  106. -- Testing ftruncate(): truncate to smaller size and display the file content --
  107. int(1024)
  108. int(0)
  109. File content after truncating file to 15 size : string(15) "222222222222222"
  110. int(0)
  111. bool(false)
  112. int(15)
  113. -- Testing ftruncate() with file opening using w mode --
  114. -- Testing ftruncate(): truncate to smaller size and display the file content --
  115. int(1024)
  116. int(0)
  117. File content after truncating file to 15 size : string(15) "222222222222222"
  118. int(0)
  119. bool(false)
  120. int(15)
  121. -- Testing ftruncate() with file opening using wb mode --
  122. -- Testing ftruncate(): truncate to smaller size and display the file content --
  123. int(1024)
  124. int(0)
  125. File content after truncating file to 15 size : string(15) "222222222222222"
  126. int(0)
  127. bool(false)
  128. int(15)
  129. -- Testing ftruncate() with file opening using wt mode --
  130. -- Testing ftruncate(): truncate to smaller size and display the file content --
  131. int(1024)
  132. int(0)
  133. File content after truncating file to 15 size : string(15) "222222222222222"
  134. int(0)
  135. bool(false)
  136. int(15)
  137. -- Testing ftruncate() with file opening using w+ mode --
  138. -- Testing ftruncate(): truncate to smaller size and display the file content --
  139. int(1024)
  140. int(0)
  141. File content after truncating file to 15 size : string(15) "222222222222222"
  142. int(0)
  143. bool(false)
  144. int(15)
  145. -- Testing ftruncate() with file opening using w+b mode --
  146. -- Testing ftruncate(): truncate to smaller size and display the file content --
  147. int(1024)
  148. int(0)
  149. File content after truncating file to 15 size : string(15) "222222222222222"
  150. int(0)
  151. bool(false)
  152. int(15)
  153. -- Testing ftruncate() with file opening using w+t mode --
  154. -- Testing ftruncate(): truncate to smaller size and display the file content --
  155. int(1024)
  156. int(0)
  157. File content after truncating file to 15 size : string(15) "222222222222222"
  158. int(0)
  159. bool(false)
  160. int(15)
  161. -- Testing ftruncate() with file opening using x mode --
  162. -- Testing ftruncate(): truncate to smaller size and display the file content --
  163. int(1024)
  164. int(0)
  165. File content after truncating file to 15 size : string(15) "222222222222222"
  166. int(0)
  167. bool(false)
  168. int(15)
  169. -- Testing ftruncate() with file opening using xb mode --
  170. -- Testing ftruncate(): truncate to smaller size and display the file content --
  171. int(1024)
  172. int(0)
  173. File content after truncating file to 15 size : string(15) "222222222222222"
  174. int(0)
  175. bool(false)
  176. int(15)
  177. -- Testing ftruncate() with file opening using xt mode --
  178. -- Testing ftruncate(): truncate to smaller size and display the file content --
  179. int(1024)
  180. int(0)
  181. File content after truncating file to 15 size : string(15) "222222222222222"
  182. int(0)
  183. bool(false)
  184. int(15)
  185. -- Testing ftruncate() with file opening using x+ mode --
  186. -- Testing ftruncate(): truncate to smaller size and display the file content --
  187. int(1024)
  188. int(0)
  189. File content after truncating file to 15 size : string(15) "222222222222222"
  190. int(0)
  191. bool(false)
  192. int(15)
  193. -- Testing ftruncate() with file opening using x+b mode --
  194. -- Testing ftruncate(): truncate to smaller size and display the file content --
  195. int(1024)
  196. int(0)
  197. File content after truncating file to 15 size : string(15) "222222222222222"
  198. int(0)
  199. bool(false)
  200. int(15)
  201. -- Testing ftruncate() with file opening using x+t mode --
  202. -- Testing ftruncate(): truncate to smaller size and display the file content --
  203. int(1024)
  204. int(0)
  205. File content after truncating file to 15 size : string(15) "222222222222222"
  206. int(0)
  207. bool(false)
  208. int(15)
  209. -- Testing ftruncate() with file opening using a mode --
  210. -- Testing ftruncate(): truncate to smaller size and display the file content --
  211. int(1024)
  212. int(0)
  213. File content after truncating file to 15 size : string(15) "222222222222222"
  214. int(0)
  215. bool(false)
  216. int(15)
  217. -- Testing ftruncate() with file opening using ab mode --
  218. -- Testing ftruncate(): truncate to smaller size and display the file content --
  219. int(1024)
  220. int(0)
  221. File content after truncating file to 15 size : string(15) "222222222222222"
  222. int(0)
  223. bool(false)
  224. int(15)
  225. -- Testing ftruncate() with file opening using at mode --
  226. -- Testing ftruncate(): truncate to smaller size and display the file content --
  227. int(1024)
  228. int(0)
  229. File content after truncating file to 15 size : string(15) "222222222222222"
  230. int(0)
  231. bool(false)
  232. int(15)
  233. -- Testing ftruncate() with file opening using a+ mode --
  234. -- Testing ftruncate(): truncate to smaller size and display the file content --
  235. int(1024)
  236. int(0)
  237. File content after truncating file to 15 size : string(15) "222222222222222"
  238. int(0)
  239. bool(false)
  240. int(15)
  241. -- Testing ftruncate() with file opening using a+b mode --
  242. -- Testing ftruncate(): truncate to smaller size and display the file content --
  243. int(1024)
  244. int(0)
  245. File content after truncating file to 15 size : string(15) "222222222222222"
  246. int(0)
  247. bool(false)
  248. int(15)
  249. -- Testing ftruncate() with file opening using a+t mode --
  250. -- Testing ftruncate(): truncate to smaller size and display the file content --
  251. int(1024)
  252. int(0)
  253. File content after truncating file to 15 size : string(15) "222222222222222"
  254. int(0)
  255. bool(false)
  256. int(15)
  257. -- Testing ftruncate() with file having data of type text_with_new_line --
  258. -- Testing ftruncate() with file opening using r mode --
  259. -- Testing ftruncate(): truncate to smaller size and display the file content --
  260. int(1024)
  261. int(0)
  262. int(0)
  263. bool(false)
  264. int(1024)
  265. -- Testing ftruncate() with file opening using rb mode --
  266. -- Testing ftruncate(): truncate to smaller size and display the file content --
  267. int(1024)
  268. int(0)
  269. int(0)
  270. bool(false)
  271. int(1024)
  272. -- Testing ftruncate() with file opening using rt mode --
  273. -- Testing ftruncate(): truncate to smaller size and display the file content --
  274. int(1024)
  275. int(0)
  276. int(0)
  277. bool(false)
  278. int(1024)
  279. -- Testing ftruncate() with file opening using r+ mode --
  280. -- Testing ftruncate(): truncate to smaller size and display the file content --
  281. int(1024)
  282. int(0)
  283. File content after truncating file to 15 size : string(15) "line
  284. line of te"
  285. int(0)
  286. bool(false)
  287. int(15)
  288. -- Testing ftruncate() with file opening using r+b mode --
  289. -- Testing ftruncate(): truncate to smaller size and display the file content --
  290. int(1024)
  291. int(0)
  292. File content after truncating file to 15 size : string(15) "line
  293. line of te"
  294. int(0)
  295. bool(false)
  296. int(15)
  297. -- Testing ftruncate() with file opening using r+t mode --
  298. -- Testing ftruncate(): truncate to smaller size and display the file content --
  299. int(1024)
  300. int(0)
  301. File content after truncating file to 15 size : string(15) "line
  302. line of te"
  303. int(0)
  304. bool(false)
  305. int(15)
  306. -- Testing ftruncate() with file opening using w mode --
  307. -- Testing ftruncate(): truncate to smaller size and display the file content --
  308. int(1024)
  309. int(0)
  310. File content after truncating file to 15 size : string(15) "line
  311. line of te"
  312. int(0)
  313. bool(false)
  314. int(15)
  315. -- Testing ftruncate() with file opening using wb mode --
  316. -- Testing ftruncate(): truncate to smaller size and display the file content --
  317. int(1024)
  318. int(0)
  319. File content after truncating file to 15 size : string(15) "line
  320. line of te"
  321. int(0)
  322. bool(false)
  323. int(15)
  324. -- Testing ftruncate() with file opening using wt mode --
  325. -- Testing ftruncate(): truncate to smaller size and display the file content --
  326. int(1024)
  327. int(0)
  328. File content after truncating file to 15 size : string(15) "line
  329. line of te"
  330. int(0)
  331. bool(false)
  332. int(15)
  333. -- Testing ftruncate() with file opening using w+ mode --
  334. -- Testing ftruncate(): truncate to smaller size and display the file content --
  335. int(1024)
  336. int(0)
  337. File content after truncating file to 15 size : string(15) "line
  338. line of te"
  339. int(0)
  340. bool(false)
  341. int(15)
  342. -- Testing ftruncate() with file opening using w+b mode --
  343. -- Testing ftruncate(): truncate to smaller size and display the file content --
  344. int(1024)
  345. int(0)
  346. File content after truncating file to 15 size : string(15) "line
  347. line of te"
  348. int(0)
  349. bool(false)
  350. int(15)
  351. -- Testing ftruncate() with file opening using w+t mode --
  352. -- Testing ftruncate(): truncate to smaller size and display the file content --
  353. int(1024)
  354. int(0)
  355. File content after truncating file to 15 size : string(15) "line
  356. line of te"
  357. int(0)
  358. bool(false)
  359. int(15)
  360. -- Testing ftruncate() with file opening using x mode --
  361. -- Testing ftruncate(): truncate to smaller size and display the file content --
  362. int(1024)
  363. int(0)
  364. File content after truncating file to 15 size : string(15) "line
  365. line of te"
  366. int(0)
  367. bool(false)
  368. int(15)
  369. -- Testing ftruncate() with file opening using xb mode --
  370. -- Testing ftruncate(): truncate to smaller size and display the file content --
  371. int(1024)
  372. int(0)
  373. File content after truncating file to 15 size : string(15) "line
  374. line of te"
  375. int(0)
  376. bool(false)
  377. int(15)
  378. -- Testing ftruncate() with file opening using xt mode --
  379. -- Testing ftruncate(): truncate to smaller size and display the file content --
  380. int(1024)
  381. int(0)
  382. File content after truncating file to 15 size : string(15) "line
  383. line of te"
  384. int(0)
  385. bool(false)
  386. int(15)
  387. -- Testing ftruncate() with file opening using x+ mode --
  388. -- Testing ftruncate(): truncate to smaller size and display the file content --
  389. int(1024)
  390. int(0)
  391. File content after truncating file to 15 size : string(15) "line
  392. line of te"
  393. int(0)
  394. bool(false)
  395. int(15)
  396. -- Testing ftruncate() with file opening using x+b mode --
  397. -- Testing ftruncate(): truncate to smaller size and display the file content --
  398. int(1024)
  399. int(0)
  400. File content after truncating file to 15 size : string(15) "line
  401. line of te"
  402. int(0)
  403. bool(false)
  404. int(15)
  405. -- Testing ftruncate() with file opening using x+t mode --
  406. -- Testing ftruncate(): truncate to smaller size and display the file content --
  407. int(1024)
  408. int(0)
  409. File content after truncating file to 15 size : string(15) "line
  410. line of te"
  411. int(0)
  412. bool(false)
  413. int(15)
  414. -- Testing ftruncate() with file opening using a mode --
  415. -- Testing ftruncate(): truncate to smaller size and display the file content --
  416. int(1024)
  417. int(0)
  418. File content after truncating file to 15 size : string(15) "line
  419. line of te"
  420. int(0)
  421. bool(false)
  422. int(15)
  423. -- Testing ftruncate() with file opening using ab mode --
  424. -- Testing ftruncate(): truncate to smaller size and display the file content --
  425. int(1024)
  426. int(0)
  427. File content after truncating file to 15 size : string(15) "line
  428. line of te"
  429. int(0)
  430. bool(false)
  431. int(15)
  432. -- Testing ftruncate() with file opening using at mode --
  433. -- Testing ftruncate(): truncate to smaller size and display the file content --
  434. int(1024)
  435. int(0)
  436. File content after truncating file to 15 size : string(15) "line
  437. line of te"
  438. int(0)
  439. bool(false)
  440. int(15)
  441. -- Testing ftruncate() with file opening using a+ mode --
  442. -- Testing ftruncate(): truncate to smaller size and display the file content --
  443. int(1024)
  444. int(0)
  445. File content after truncating file to 15 size : string(15) "line
  446. line of te"
  447. int(0)
  448. bool(false)
  449. int(15)
  450. -- Testing ftruncate() with file opening using a+b mode --
  451. -- Testing ftruncate(): truncate to smaller size and display the file content --
  452. int(1024)
  453. int(0)
  454. File content after truncating file to 15 size : string(15) "line
  455. line of te"
  456. int(0)
  457. bool(false)
  458. int(15)
  459. -- Testing ftruncate() with file opening using a+t mode --
  460. -- Testing ftruncate(): truncate to smaller size and display the file content --
  461. int(1024)
  462. int(0)
  463. File content after truncating file to 15 size : string(15) "line
  464. line of te"
  465. int(0)
  466. bool(false)
  467. int(15)
  468. Done