1--TEST--
2Test fseek(), ftell() & rewind() functions : usage variations - all r & a modes, default whence
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() without using argument whence
21*/
22echo "*** Testing fseek(), ftell(), rewind() : default whence & all r and a modes ***\n";
23$file_modes = array( "r","rb","rt","r+","r+b","r+t",
24                     "a","ab","at","a+","a+b","a+t");
25$file_content_types = array( "text_with_new_line","alphanumeric");
26
27$offset = array(-1, 0, 1, 513); // different offsets, including negative and beyond size
28
29$filename = dirname(__FILE__)."/fseek_ftell_rewind_variation1.tmp"; // this is name of the file created by create_files()
30
31  /* open the file using $files_modes and perform fseek(),ftell() and rewind() on it */
32foreach($file_content_types as $file_content_type){
33  echo "\n-- File having data of type ". $file_content_type ." --\n";
34
35  foreach($file_modes as $file_mode) {
36    echo "-- File opened in mode ".$file_mode." --\n";
37
38    create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 512, "w", "fseek_ftell_rewind_variation"
39                      ,1,"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    echo "-- Testing fseek() without using argument whence --\n";
46    foreach($offset as $count){
47      var_dump( fseek($file_handle, $count) );
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() : default whence & all r and a modes ***
64
65-- File having data of type text_with_new_line --
66-- File opened in mode r --
67-- Testing fseek() without using argument whence --
68int(-1)
69int(0)
70bool(false)
71int(0)
72int(0)
73bool(false)
74int(0)
75int(1)
76bool(false)
77int(0)
78int(513)
79bool(false)
80int(512)
81-- File opened in mode rb --
82-- Testing fseek() without using argument whence --
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(512)
96-- File opened in mode rt --
97-- Testing fseek() without using argument whence --
98int(-1)
99int(0)
100bool(false)
101int(0)
102int(0)
103bool(false)
104int(0)
105int(1)
106bool(false)
107int(0)
108int(513)
109bool(false)
110int(512)
111-- File opened in mode r+ --
112-- Testing fseek() without using argument whence --
113int(-1)
114int(0)
115bool(false)
116int(0)
117int(0)
118bool(false)
119int(0)
120int(1)
121bool(false)
122int(0)
123int(513)
124bool(false)
125int(512)
126-- File opened in mode r+b --
127-- Testing fseek() without using argument whence --
128int(-1)
129int(0)
130bool(false)
131int(0)
132int(0)
133bool(false)
134int(0)
135int(1)
136bool(false)
137int(0)
138int(513)
139bool(false)
140int(512)
141-- File opened in mode r+t --
142-- Testing fseek() without using argument whence --
143int(-1)
144int(0)
145bool(false)
146int(0)
147int(0)
148bool(false)
149int(0)
150int(1)
151bool(false)
152int(0)
153int(513)
154bool(false)
155int(512)
156-- File opened in mode a --
157-- Testing fseek() without using argument whence --
158int(-1)
159int(0)
160bool(false)
161int(0)
162int(0)
163bool(false)
164int(0)
165int(1)
166bool(false)
167int(0)
168int(513)
169bool(false)
170int(512)
171-- File opened in mode ab --
172-- Testing fseek() without using argument whence --
173int(-1)
174int(0)
175bool(false)
176int(0)
177int(0)
178bool(false)
179int(0)
180int(1)
181bool(false)
182int(0)
183int(513)
184bool(false)
185int(512)
186-- File opened in mode at --
187-- Testing fseek() without using argument whence --
188int(-1)
189int(0)
190bool(false)
191int(0)
192int(0)
193bool(false)
194int(0)
195int(1)
196bool(false)
197int(0)
198int(513)
199bool(false)
200int(512)
201-- File opened in mode a+ --
202-- Testing fseek() without using argument whence --
203int(-1)
204int(0)
205bool(false)
206int(0)
207int(0)
208bool(false)
209int(0)
210int(1)
211bool(false)
212int(0)
213int(513)
214bool(false)
215int(512)
216-- File opened in mode a+b --
217-- Testing fseek() without using argument whence --
218int(-1)
219int(0)
220bool(false)
221int(0)
222int(0)
223bool(false)
224int(0)
225int(1)
226bool(false)
227int(0)
228int(513)
229bool(false)
230int(512)
231-- File opened in mode a+t --
232-- Testing fseek() without using argument whence --
233int(-1)
234int(0)
235bool(false)
236int(0)
237int(0)
238bool(false)
239int(0)
240int(1)
241bool(false)
242int(0)
243int(513)
244bool(false)
245int(512)
246
247-- File having data of type alphanumeric --
248-- File opened in mode r --
249-- Testing fseek() without using argument whence --
250int(-1)
251int(0)
252bool(false)
253int(0)
254int(0)
255bool(false)
256int(0)
257int(1)
258bool(false)
259int(0)
260int(513)
261bool(false)
262int(512)
263-- File opened in mode rb --
264-- Testing fseek() without using argument whence --
265int(-1)
266int(0)
267bool(false)
268int(0)
269int(0)
270bool(false)
271int(0)
272int(1)
273bool(false)
274int(0)
275int(513)
276bool(false)
277int(512)
278-- File opened in mode rt --
279-- Testing fseek() without using argument whence --
280int(-1)
281int(0)
282bool(false)
283int(0)
284int(0)
285bool(false)
286int(0)
287int(1)
288bool(false)
289int(0)
290int(513)
291bool(false)
292int(512)
293-- File opened in mode r+ --
294-- Testing fseek() without using argument whence --
295int(-1)
296int(0)
297bool(false)
298int(0)
299int(0)
300bool(false)
301int(0)
302int(1)
303bool(false)
304int(0)
305int(513)
306bool(false)
307int(512)
308-- File opened in mode r+b --
309-- Testing fseek() without using argument whence --
310int(-1)
311int(0)
312bool(false)
313int(0)
314int(0)
315bool(false)
316int(0)
317int(1)
318bool(false)
319int(0)
320int(513)
321bool(false)
322int(512)
323-- File opened in mode r+t --
324-- Testing fseek() without using argument whence --
325int(-1)
326int(0)
327bool(false)
328int(0)
329int(0)
330bool(false)
331int(0)
332int(1)
333bool(false)
334int(0)
335int(513)
336bool(false)
337int(512)
338-- File opened in mode a --
339-- Testing fseek() without using argument whence --
340int(-1)
341int(0)
342bool(false)
343int(0)
344int(0)
345bool(false)
346int(0)
347int(1)
348bool(false)
349int(0)
350int(513)
351bool(false)
352int(512)
353-- File opened in mode ab --
354-- Testing fseek() without using argument whence --
355int(-1)
356int(0)
357bool(false)
358int(0)
359int(0)
360bool(false)
361int(0)
362int(1)
363bool(false)
364int(0)
365int(513)
366bool(false)
367int(512)
368-- File opened in mode at --
369-- Testing fseek() without using argument whence --
370int(-1)
371int(0)
372bool(false)
373int(0)
374int(0)
375bool(false)
376int(0)
377int(1)
378bool(false)
379int(0)
380int(513)
381bool(false)
382int(512)
383-- File opened in mode a+ --
384-- Testing fseek() without using argument whence --
385int(-1)
386int(0)
387bool(false)
388int(0)
389int(0)
390bool(false)
391int(0)
392int(1)
393bool(false)
394int(0)
395int(513)
396bool(false)
397int(512)
398-- File opened in mode a+b --
399-- Testing fseek() without using argument whence --
400int(-1)
401int(0)
402bool(false)
403int(0)
404int(0)
405bool(false)
406int(0)
407int(1)
408bool(false)
409int(0)
410int(513)
411bool(false)
412int(512)
413-- File opened in mode a+t --
414-- Testing fseek() without using argument whence --
415int(-1)
416int(0)
417bool(false)
418int(0)
419int(0)
420bool(false)
421int(0)
422int(1)
423bool(false)
424int(0)
425int(513)
426bool(false)
427int(512)
428Done
429