1--TEST--
2Test fread() function : usage variations - read beyond file size, read/write 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+","a+b","a+t",
56                    "w+","w+b","w+t",
57                    "x+","x+b","x+t");
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", 3);
69    }
70
71    $filename = dirname(__FILE__)."/fread_variation3.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      $data_to_be_written="";
81      fill_file($file_handle, $file_content_type, 1024);
82    }
83
84    rewind($file_handle);
85
86    // read file by giving size more than its size
87    echo "-- Reading beyond filesize, expeceted : 1024 bytes --\n";
88    rewind($file_handle);
89    $data_from_file = check_read($file_handle, 1030, ( strstr($file_mode, "+") ? 1024 : 1024) );
90    if ( $data_from_file != false)
91      var_dump( md5($data_from_file) );
92
93    rewind($file_handle);
94    echo "-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --\n";
95    // try fread when file pointer at end
96    fseek($file_handle, 0, SEEK_END);
97    //reading file when file pointer at end
98    $data_from_file = check_read($file_handle, 10, 0);
99    if ( $data_from_file != false)
100      var_dump( md5($data_from_file) );
101
102    // now close the file
103    fclose($file_handle);
104
105    // delete the file created
106    delete_file($filename); // delete file
107  } // end of inner foreach loop
108}// end of outer foreach loop
109
110echo"Done\n";
111?>
112--EXPECTF--
113*** Testing fread() : usage variations ***
114
115-- Testing fread() with file having content of type numeric --
116-- File opened in mode a+ --
117-- Reading beyond filesize, expeceted : 1024 bytes --
118int(0)
119bool(false)
120Reading 1030 bytes from file, expecting 1024 bytes ... OK
121int(1024)
122bool(true)
123string(32) "950b7457d1deb6332f2fc5d42f3129d6"
124-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
125int(1024)
126bool(false)
127Reading 10 bytes from file, expecting 0 bytes ... OK
128int(1024)
129bool(true)
130-- File opened in mode a+b --
131-- Reading beyond filesize, expeceted : 1024 bytes --
132int(0)
133bool(false)
134Reading 1030 bytes from file, expecting 1024 bytes ... OK
135int(1024)
136bool(true)
137string(32) "950b7457d1deb6332f2fc5d42f3129d6"
138-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
139int(1024)
140bool(false)
141Reading 10 bytes from file, expecting 0 bytes ... OK
142int(1024)
143bool(true)
144-- File opened in mode a+t --
145-- Reading beyond filesize, expeceted : 1024 bytes --
146int(0)
147bool(false)
148Reading 1030 bytes from file, expecting 1024 bytes ... OK
149int(1024)
150bool(true)
151string(32) "950b7457d1deb6332f2fc5d42f3129d6"
152-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
153int(1024)
154bool(false)
155Reading 10 bytes from file, expecting 0 bytes ... OK
156int(1024)
157bool(true)
158-- File opened in mode w+ --
159-- Reading beyond filesize, expeceted : 1024 bytes --
160int(0)
161bool(false)
162Reading 1030 bytes from file, expecting 1024 bytes ... OK
163int(1024)
164bool(true)
165string(32) "950b7457d1deb6332f2fc5d42f3129d6"
166-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
167int(1024)
168bool(false)
169Reading 10 bytes from file, expecting 0 bytes ... OK
170int(1024)
171bool(true)
172-- File opened in mode w+b --
173-- Reading beyond filesize, expeceted : 1024 bytes --
174int(0)
175bool(false)
176Reading 1030 bytes from file, expecting 1024 bytes ... OK
177int(1024)
178bool(true)
179string(32) "950b7457d1deb6332f2fc5d42f3129d6"
180-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
181int(1024)
182bool(false)
183Reading 10 bytes from file, expecting 0 bytes ... OK
184int(1024)
185bool(true)
186-- File opened in mode w+t --
187-- Reading beyond filesize, expeceted : 1024 bytes --
188int(0)
189bool(false)
190Reading 1030 bytes from file, expecting 1024 bytes ... OK
191int(1024)
192bool(true)
193string(32) "950b7457d1deb6332f2fc5d42f3129d6"
194-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
195int(1024)
196bool(false)
197Reading 10 bytes from file, expecting 0 bytes ... OK
198int(1024)
199bool(true)
200-- File opened in mode x+ --
201-- Reading beyond filesize, expeceted : 1024 bytes --
202int(0)
203bool(false)
204Reading 1030 bytes from file, expecting 1024 bytes ... OK
205int(1024)
206bool(true)
207string(32) "950b7457d1deb6332f2fc5d42f3129d6"
208-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
209int(1024)
210bool(false)
211Reading 10 bytes from file, expecting 0 bytes ... OK
212int(1024)
213bool(true)
214-- File opened in mode x+b --
215-- Reading beyond filesize, expeceted : 1024 bytes --
216int(0)
217bool(false)
218Reading 1030 bytes from file, expecting 1024 bytes ... OK
219int(1024)
220bool(true)
221string(32) "950b7457d1deb6332f2fc5d42f3129d6"
222-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
223int(1024)
224bool(false)
225Reading 10 bytes from file, expecting 0 bytes ... OK
226int(1024)
227bool(true)
228-- File opened in mode x+t --
229-- Reading beyond filesize, expeceted : 1024 bytes --
230int(0)
231bool(false)
232Reading 1030 bytes from file, expecting 1024 bytes ... OK
233int(1024)
234bool(true)
235string(32) "950b7457d1deb6332f2fc5d42f3129d6"
236-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
237int(1024)
238bool(false)
239Reading 10 bytes from file, expecting 0 bytes ... OK
240int(1024)
241bool(true)
242
243-- Testing fread() with file having content of type text --
244-- File opened in mode a+ --
245-- Reading beyond filesize, expeceted : 1024 bytes --
246int(0)
247bool(false)
248Reading 1030 bytes from file, expecting 1024 bytes ... OK
249int(1024)
250bool(true)
251string(32) "e486000c4c8452774f746a27658d87fa"
252-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
253int(1024)
254bool(false)
255Reading 10 bytes from file, expecting 0 bytes ... OK
256int(1024)
257bool(true)
258-- File opened in mode a+b --
259-- Reading beyond filesize, expeceted : 1024 bytes --
260int(0)
261bool(false)
262Reading 1030 bytes from file, expecting 1024 bytes ... OK
263int(1024)
264bool(true)
265string(32) "e486000c4c8452774f746a27658d87fa"
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(true)
272-- File opened in mode a+t --
273-- Reading beyond filesize, expeceted : 1024 bytes --
274int(0)
275bool(false)
276Reading 1030 bytes from file, expecting 1024 bytes ... OK
277int(1024)
278bool(true)
279string(32) "e486000c4c8452774f746a27658d87fa"
280-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
281int(1024)
282bool(false)
283Reading 10 bytes from file, expecting 0 bytes ... OK
284int(1024)
285bool(true)
286-- File opened in mode w+ --
287-- Reading beyond filesize, expeceted : 1024 bytes --
288int(0)
289bool(false)
290Reading 1030 bytes from file, expecting 1024 bytes ... OK
291int(1024)
292bool(true)
293string(32) "e486000c4c8452774f746a27658d87fa"
294-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
295int(1024)
296bool(false)
297Reading 10 bytes from file, expecting 0 bytes ... OK
298int(1024)
299bool(true)
300-- File opened in mode w+b --
301-- Reading beyond filesize, expeceted : 1024 bytes --
302int(0)
303bool(false)
304Reading 1030 bytes from file, expecting 1024 bytes ... OK
305int(1024)
306bool(true)
307string(32) "e486000c4c8452774f746a27658d87fa"
308-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
309int(1024)
310bool(false)
311Reading 10 bytes from file, expecting 0 bytes ... OK
312int(1024)
313bool(true)
314-- File opened in mode w+t --
315-- Reading beyond filesize, expeceted : 1024 bytes --
316int(0)
317bool(false)
318Reading 1030 bytes from file, expecting 1024 bytes ... OK
319int(1024)
320bool(true)
321string(32) "e486000c4c8452774f746a27658d87fa"
322-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
323int(1024)
324bool(false)
325Reading 10 bytes from file, expecting 0 bytes ... OK
326int(1024)
327bool(true)
328-- File opened in mode x+ --
329-- Reading beyond filesize, expeceted : 1024 bytes --
330int(0)
331bool(false)
332Reading 1030 bytes from file, expecting 1024 bytes ... OK
333int(1024)
334bool(true)
335string(32) "e486000c4c8452774f746a27658d87fa"
336-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
337int(1024)
338bool(false)
339Reading 10 bytes from file, expecting 0 bytes ... OK
340int(1024)
341bool(true)
342-- File opened in mode x+b --
343-- Reading beyond filesize, expeceted : 1024 bytes --
344int(0)
345bool(false)
346Reading 1030 bytes from file, expecting 1024 bytes ... OK
347int(1024)
348bool(true)
349string(32) "e486000c4c8452774f746a27658d87fa"
350-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
351int(1024)
352bool(false)
353Reading 10 bytes from file, expecting 0 bytes ... OK
354int(1024)
355bool(true)
356-- File opened in mode x+t --
357-- Reading beyond filesize, expeceted : 1024 bytes --
358int(0)
359bool(false)
360Reading 1030 bytes from file, expecting 1024 bytes ... OK
361int(1024)
362bool(true)
363string(32) "e486000c4c8452774f746a27658d87fa"
364-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
365int(1024)
366bool(false)
367Reading 10 bytes from file, expecting 0 bytes ... OK
368int(1024)
369bool(true)
370
371-- Testing fread() with file having content of type text_with_new_line --
372-- File opened in mode a+ --
373-- Reading beyond filesize, expeceted : 1024 bytes --
374int(0)
375bool(false)
376Reading 1030 bytes from file, expecting 1024 bytes ... OK
377int(1024)
378bool(true)
379string(32) "b09c8026a64a88d36d4c2f17983964bb"
380-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
381int(1024)
382bool(false)
383Reading 10 bytes from file, expecting 0 bytes ... OK
384int(1024)
385bool(true)
386-- File opened in mode a+b --
387-- Reading beyond filesize, expeceted : 1024 bytes --
388int(0)
389bool(false)
390Reading 1030 bytes from file, expecting 1024 bytes ... OK
391int(1024)
392bool(true)
393string(32) "b09c8026a64a88d36d4c2f17983964bb"
394-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
395int(1024)
396bool(false)
397Reading 10 bytes from file, expecting 0 bytes ... OK
398int(1024)
399bool(true)
400-- File opened in mode a+t --
401-- Reading beyond filesize, expeceted : 1024 bytes --
402int(0)
403bool(false)
404Reading 1030 bytes from file, expecting 1024 bytes ... OK
405int(1024)
406bool(true)
407string(32) "b09c8026a64a88d36d4c2f17983964bb"
408-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
409int(1024)
410bool(false)
411Reading 10 bytes from file, expecting 0 bytes ... OK
412int(1024)
413bool(true)
414-- File opened in mode w+ --
415-- Reading beyond filesize, expeceted : 1024 bytes --
416int(0)
417bool(false)
418Reading 1030 bytes from file, expecting 1024 bytes ... OK
419int(1024)
420bool(true)
421string(32) "b09c8026a64a88d36d4c2f17983964bb"
422-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
423int(1024)
424bool(false)
425Reading 10 bytes from file, expecting 0 bytes ... OK
426int(1024)
427bool(true)
428-- File opened in mode w+b --
429-- Reading beyond filesize, expeceted : 1024 bytes --
430int(0)
431bool(false)
432Reading 1030 bytes from file, expecting 1024 bytes ... OK
433int(1024)
434bool(true)
435string(32) "b09c8026a64a88d36d4c2f17983964bb"
436-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
437int(1024)
438bool(false)
439Reading 10 bytes from file, expecting 0 bytes ... OK
440int(1024)
441bool(true)
442-- File opened in mode w+t --
443-- Reading beyond filesize, expeceted : 1024 bytes --
444int(0)
445bool(false)
446Reading 1030 bytes from file, expecting 1024 bytes ... OK
447int(1024)
448bool(true)
449string(32) "b09c8026a64a88d36d4c2f17983964bb"
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(true)
456-- File opened in mode x+ --
457-- Reading beyond filesize, expeceted : 1024 bytes --
458int(0)
459bool(false)
460Reading 1030 bytes from file, expecting 1024 bytes ... OK
461int(1024)
462bool(true)
463string(32) "b09c8026a64a88d36d4c2f17983964bb"
464-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
465int(1024)
466bool(false)
467Reading 10 bytes from file, expecting 0 bytes ... OK
468int(1024)
469bool(true)
470-- File opened in mode x+b --
471-- Reading beyond filesize, expeceted : 1024 bytes --
472int(0)
473bool(false)
474Reading 1030 bytes from file, expecting 1024 bytes ... OK
475int(1024)
476bool(true)
477string(32) "b09c8026a64a88d36d4c2f17983964bb"
478-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
479int(1024)
480bool(false)
481Reading 10 bytes from file, expecting 0 bytes ... OK
482int(1024)
483bool(true)
484-- File opened in mode x+t --
485-- Reading beyond filesize, expeceted : 1024 bytes --
486int(0)
487bool(false)
488Reading 1030 bytes from file, expecting 1024 bytes ... OK
489int(1024)
490bool(true)
491string(32) "b09c8026a64a88d36d4c2f17983964bb"
492-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --
493int(1024)
494bool(false)
495Reading 10 bytes from file, expecting 0 bytes ... OK
496int(1024)
497bool(true)
498Done