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