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