1--TEST--
2Test fread() function : usage variations - read beyond file size, write only mode
3--SKIPIF--
4<?php
5if (substr(PHP_OS, 0, 3) == 'WIN') {
6  die('skip.. Not valid for Windows');
7}
8?>
9--FILE--
10<?php
11/*
12 Prototype: string fread ( resource $handle [, int $length] );
13 Description: reads up to length bytes from the file pointer referenced by handle.
14   Reading stops when up to length bytes have been read, EOF (end of file) is
15   reached, (for network streams) when a packet becomes available, or (after
16   opening userspace stream) when 8192 bytes have been read whichever comes first.
17*/
18
19// include the file.inc for common functions for test
20include ("file.inc");
21
22/* Function : function check_read(resource $file_handle, int $read_size, int $expect_size)
23   Description : Read data from file of size $read_size and verifies that $expected_size no. of
24                 bytes are read.
25     $file_handle : File Handle
26     $read_size   : No. of bytes to be read.
27     $expect_size : Expected data length
28   Returns: returns the data read
29*/
30function check_read($file_handle, $read_size, $expect_size) {
31  // print file pointer position before read
32  var_dump( ftell($file_handle) );
33  var_dump( feof($file_handle) );
34
35  // read the data of size $read_size
36  echo "Reading $read_size bytes from file, expecting $expect_size bytes ... ";
37  $data_from_file = fread($file_handle, $read_size);
38
39  // check if data read is of expected size
40  if ( strlen($data_from_file) == $expect_size)
41    echo "OK\n";
42  else
43    echo "Error reading file, total number of bytes read = ".strlen($data_from_file)."\n";
44
45  // file pointer position after read
46  var_dump( ftell($file_handle) );
47  // check if file pointer at eof()
48  var_dump( feof($file_handle) );
49
50  return $data_from_file;
51}
52
53echo "*** Testing fread() : usage variations ***\n";
54
55$file_modes = array("a","ab","at",
56                    "w","wb","wt",
57                    "x","xb","xt" );
58
59$file_content_types = array("numeric","text","text_with_new_line");
60
61foreach($file_content_types as $file_content_type) {
62  echo "\n-- Testing fread() with file having content of type ". $file_content_type ." --\n";
63
64  /* open the file using $files_modes and perform fread() on it */
65  foreach($file_modes as $file_mode) {
66    if(!strstr($file_mode,"x")){
67       /* create files with $file_content_type */
68       create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 1, "w", "fread_variation", 4);
69    }
70
71    $filename = dirname(__FILE__)."/fread_variation4.tmp"; // this is name of the file created by create_files()
72    echo "-- File opened in mode ".$file_mode." --\n";
73    $file_handle = fopen($filename, $file_mode);
74    if (!$file_handle) {
75       echo "Error: failed to fopen() file: $filename!";
76       exit();
77    }
78
79    if(strstr($file_mode,"w") || strstr($file_mode,"x") ) {
80      fill_file($file_handle, $file_content_type, 1024);
81    }
82
83    rewind($file_handle);
84    echo "-- Reading beyond filesize, expeceted : 1024 bytes --\n";
85    // read file by giving size more than its size
86    rewind($file_handle);
87    $data_from_file = check_read($file_handle, 1030, ( strstr($file_mode, "+") ? 1024 : 0) );
88    if ( $data_from_file != false)
89      var_dump( md5($data_from_file) );
90
91    echo "-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --\n";
92    rewind($file_handle);
93    // try fread when file pointer at end
94    fseek($file_handle, 0, SEEK_END);
95    //reading file when file pointer at end
96    $data_from_file = check_read($file_handle, 10, 0);
97    if ( $data_from_file != false)
98      var_dump( md5($data_from_file) );
99
100    // now close the file
101    fclose($file_handle);
102
103    // delete the file created
104    delete_file($filename); // delete file
105  } // end of inner foreach loop
106}// end of outer foreach loop
107
108echo"Done\n";
109?>
110--EXPECT--
111*** Testing fread() : usage variations ***
112
113-- Testing fread() with file having content of type numeric --
114-- File opened in mode a --
115-- Reading beyond filesize, expeceted : 1024 bytes --
116int(0)
117bool(false)
118Reading 1030 bytes from file, expecting 0 bytes ... OK
119int(0)
120bool(false)
121-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
122int(1024)
123bool(false)
124Reading 10 bytes from file, expecting 0 bytes ... OK
125int(1024)
126bool(false)
127-- File opened in mode ab --
128-- Reading beyond filesize, expeceted : 1024 bytes --
129int(0)
130bool(false)
131Reading 1030 bytes from file, expecting 0 bytes ... OK
132int(0)
133bool(false)
134-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
135int(1024)
136bool(false)
137Reading 10 bytes from file, expecting 0 bytes ... OK
138int(1024)
139bool(false)
140-- File opened in mode at --
141-- Reading beyond filesize, expeceted : 1024 bytes --
142int(0)
143bool(false)
144Reading 1030 bytes from file, expecting 0 bytes ... OK
145int(0)
146bool(false)
147-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
148int(1024)
149bool(false)
150Reading 10 bytes from file, expecting 0 bytes ... OK
151int(1024)
152bool(false)
153-- File opened in mode w --
154-- Reading beyond filesize, expeceted : 1024 bytes --
155int(0)
156bool(false)
157Reading 1030 bytes from file, expecting 0 bytes ... OK
158int(0)
159bool(false)
160-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
161int(1024)
162bool(false)
163Reading 10 bytes from file, expecting 0 bytes ... OK
164int(1024)
165bool(false)
166-- File opened in mode wb --
167-- Reading beyond filesize, expeceted : 1024 bytes --
168int(0)
169bool(false)
170Reading 1030 bytes from file, expecting 0 bytes ... OK
171int(0)
172bool(false)
173-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
174int(1024)
175bool(false)
176Reading 10 bytes from file, expecting 0 bytes ... OK
177int(1024)
178bool(false)
179-- File opened in mode wt --
180-- Reading beyond filesize, expeceted : 1024 bytes --
181int(0)
182bool(false)
183Reading 1030 bytes from file, expecting 0 bytes ... OK
184int(0)
185bool(false)
186-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
187int(1024)
188bool(false)
189Reading 10 bytes from file, expecting 0 bytes ... OK
190int(1024)
191bool(false)
192-- File opened in mode x --
193-- Reading beyond filesize, expeceted : 1024 bytes --
194int(0)
195bool(false)
196Reading 1030 bytes from file, expecting 0 bytes ... OK
197int(0)
198bool(false)
199-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
200int(1024)
201bool(false)
202Reading 10 bytes from file, expecting 0 bytes ... OK
203int(1024)
204bool(false)
205-- File opened in mode xb --
206-- Reading beyond filesize, expeceted : 1024 bytes --
207int(0)
208bool(false)
209Reading 1030 bytes from file, expecting 0 bytes ... OK
210int(0)
211bool(false)
212-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
213int(1024)
214bool(false)
215Reading 10 bytes from file, expecting 0 bytes ... OK
216int(1024)
217bool(false)
218-- File opened in mode xt --
219-- Reading beyond filesize, expeceted : 1024 bytes --
220int(0)
221bool(false)
222Reading 1030 bytes from file, expecting 0 bytes ... OK
223int(0)
224bool(false)
225-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
226int(1024)
227bool(false)
228Reading 10 bytes from file, expecting 0 bytes ... OK
229int(1024)
230bool(false)
231
232-- Testing fread() with file having content of type text --
233-- File opened in mode a --
234-- Reading beyond filesize, expeceted : 1024 bytes --
235int(0)
236bool(false)
237Reading 1030 bytes from file, expecting 0 bytes ... OK
238int(0)
239bool(false)
240-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
241int(1024)
242bool(false)
243Reading 10 bytes from file, expecting 0 bytes ... OK
244int(1024)
245bool(false)
246-- File opened in mode ab --
247-- Reading beyond filesize, expeceted : 1024 bytes --
248int(0)
249bool(false)
250Reading 1030 bytes from file, expecting 0 bytes ... OK
251int(0)
252bool(false)
253-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
254int(1024)
255bool(false)
256Reading 10 bytes from file, expecting 0 bytes ... OK
257int(1024)
258bool(false)
259-- File opened in mode at --
260-- Reading beyond filesize, expeceted : 1024 bytes --
261int(0)
262bool(false)
263Reading 1030 bytes from file, expecting 0 bytes ... OK
264int(0)
265bool(false)
266-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
267int(1024)
268bool(false)
269Reading 10 bytes from file, expecting 0 bytes ... OK
270int(1024)
271bool(false)
272-- File opened in mode w --
273-- Reading beyond filesize, expeceted : 1024 bytes --
274int(0)
275bool(false)
276Reading 1030 bytes from file, expecting 0 bytes ... OK
277int(0)
278bool(false)
279-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
280int(1024)
281bool(false)
282Reading 10 bytes from file, expecting 0 bytes ... OK
283int(1024)
284bool(false)
285-- File opened in mode wb --
286-- Reading beyond filesize, expeceted : 1024 bytes --
287int(0)
288bool(false)
289Reading 1030 bytes from file, expecting 0 bytes ... OK
290int(0)
291bool(false)
292-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
293int(1024)
294bool(false)
295Reading 10 bytes from file, expecting 0 bytes ... OK
296int(1024)
297bool(false)
298-- File opened in mode wt --
299-- Reading beyond filesize, expeceted : 1024 bytes --
300int(0)
301bool(false)
302Reading 1030 bytes from file, expecting 0 bytes ... OK
303int(0)
304bool(false)
305-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
306int(1024)
307bool(false)
308Reading 10 bytes from file, expecting 0 bytes ... OK
309int(1024)
310bool(false)
311-- File opened in mode x --
312-- Reading beyond filesize, expeceted : 1024 bytes --
313int(0)
314bool(false)
315Reading 1030 bytes from file, expecting 0 bytes ... OK
316int(0)
317bool(false)
318-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
319int(1024)
320bool(false)
321Reading 10 bytes from file, expecting 0 bytes ... OK
322int(1024)
323bool(false)
324-- File opened in mode xb --
325-- Reading beyond filesize, expeceted : 1024 bytes --
326int(0)
327bool(false)
328Reading 1030 bytes from file, expecting 0 bytes ... OK
329int(0)
330bool(false)
331-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
332int(1024)
333bool(false)
334Reading 10 bytes from file, expecting 0 bytes ... OK
335int(1024)
336bool(false)
337-- File opened in mode xt --
338-- Reading beyond filesize, expeceted : 1024 bytes --
339int(0)
340bool(false)
341Reading 1030 bytes from file, expecting 0 bytes ... OK
342int(0)
343bool(false)
344-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
345int(1024)
346bool(false)
347Reading 10 bytes from file, expecting 0 bytes ... OK
348int(1024)
349bool(false)
350
351-- Testing fread() with file having content of type text_with_new_line --
352-- File opened in mode a --
353-- Reading beyond filesize, expeceted : 1024 bytes --
354int(0)
355bool(false)
356Reading 1030 bytes from file, expecting 0 bytes ... OK
357int(0)
358bool(false)
359-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
360int(1024)
361bool(false)
362Reading 10 bytes from file, expecting 0 bytes ... OK
363int(1024)
364bool(false)
365-- File opened in mode ab --
366-- Reading beyond filesize, expeceted : 1024 bytes --
367int(0)
368bool(false)
369Reading 1030 bytes from file, expecting 0 bytes ... OK
370int(0)
371bool(false)
372-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
373int(1024)
374bool(false)
375Reading 10 bytes from file, expecting 0 bytes ... OK
376int(1024)
377bool(false)
378-- File opened in mode at --
379-- Reading beyond filesize, expeceted : 1024 bytes --
380int(0)
381bool(false)
382Reading 1030 bytes from file, expecting 0 bytes ... OK
383int(0)
384bool(false)
385-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
386int(1024)
387bool(false)
388Reading 10 bytes from file, expecting 0 bytes ... OK
389int(1024)
390bool(false)
391-- File opened in mode w --
392-- Reading beyond filesize, expeceted : 1024 bytes --
393int(0)
394bool(false)
395Reading 1030 bytes from file, expecting 0 bytes ... OK
396int(0)
397bool(false)
398-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
399int(1024)
400bool(false)
401Reading 10 bytes from file, expecting 0 bytes ... OK
402int(1024)
403bool(false)
404-- File opened in mode wb --
405-- Reading beyond filesize, expeceted : 1024 bytes --
406int(0)
407bool(false)
408Reading 1030 bytes from file, expecting 0 bytes ... OK
409int(0)
410bool(false)
411-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
412int(1024)
413bool(false)
414Reading 10 bytes from file, expecting 0 bytes ... OK
415int(1024)
416bool(false)
417-- File opened in mode wt --
418-- Reading beyond filesize, expeceted : 1024 bytes --
419int(0)
420bool(false)
421Reading 1030 bytes from file, expecting 0 bytes ... OK
422int(0)
423bool(false)
424-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
425int(1024)
426bool(false)
427Reading 10 bytes from file, expecting 0 bytes ... OK
428int(1024)
429bool(false)
430-- File opened in mode x --
431-- Reading beyond filesize, expeceted : 1024 bytes --
432int(0)
433bool(false)
434Reading 1030 bytes from file, expecting 0 bytes ... OK
435int(0)
436bool(false)
437-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
438int(1024)
439bool(false)
440Reading 10 bytes from file, expecting 0 bytes ... OK
441int(1024)
442bool(false)
443-- File opened in mode xb --
444-- Reading beyond filesize, expeceted : 1024 bytes --
445int(0)
446bool(false)
447Reading 1030 bytes from file, expecting 0 bytes ... OK
448int(0)
449bool(false)
450-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
451int(1024)
452bool(false)
453Reading 10 bytes from file, expecting 0 bytes ... OK
454int(1024)
455bool(false)
456-- File opened in mode xt --
457-- Reading beyond filesize, expeceted : 1024 bytes --
458int(0)
459bool(false)
460Reading 1030 bytes from file, expecting 0 bytes ... OK
461int(0)
462bool(false)
463-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
464int(1024)
465bool(false)
466Reading 10 bytes from file, expecting 0 bytes ... OK
467int(1024)
468bool(false)
469Done
470