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