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