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