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