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