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