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