1--TEST--
2Test fread() function : basic functionality
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_size(string $data, int $expect_size)
17   Description : Check the length of the data, and compare the size with $expect_size
18     $data : Text data.
19     $expect_size : Expected data length
20*/
21function check_size($data, $expect_size) {
22
23  $size=strlen($data);
24  if ( $size == $expect_size)
25    echo "OK\n";
26  else
27    echo "Error: Expected: $expect_size, Actual: $size";
28 }
29
30
31echo "*** Testing fread() basic operations ***\n";
32/*
33 test fread with file opened in "r" and "rb" mode only
34 Content with numeric and strings with it
35*/
36$file_modes = array( "r", "rb", "rt", "r+", "r+b", "r+t");
37$file_content_types = array("numeric","text","text_with_new_line","alphanumeric");
38
39 foreach($file_content_types as $file_content_type) {
40   echo "\n-- Testing fread) with file having data of type ". $file_content_type ." --\n";
41   /* create files with $file_content_type */
42   create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 1, "w", "fread_basic");
43   $filename = dirname(__FILE__)."/fread_basic1.tmp"; // this is name of the file created by create_files()
44
45  /* open the file using $files_modes and perform fread() on it */
46   for($inner_loop_counter = 0;
47       $inner_loop_counter < count($file_modes);
48       $inner_loop_counter++) {
49
50    echo "-- File opened in mode ".$file_modes[$inner_loop_counter]." --\n";
51    $file_handle = fopen($filename, $file_modes[$inner_loop_counter]);
52    if (!$file_handle) {
53       echo "Error: failed to fopen() file: $filename!";
54       exit();
55    }
56
57    /* read file by giving the acutal length, check the length and content by calculating the
58      hash using md5() function
59    */
60    /* Reading 1024 bytes from file, expecting 1024 bytes */ ;
61
62    var_dump(ftell($file_handle));
63    var_dump( feof($file_handle) );
64    echo "Reading 1024 bytes from file, expecting 1024 bytes ... ";
65    $data_from_file=fread($file_handle, 1024);
66    check_size($data_from_file,1024);
67    var_dump(ftell($file_handle));
68    var_dump( feof($file_handle) );
69    var_dump( md5($data_from_file) ); // calculate the hash and dump it
70
71    /* read file by giving size more than its size */
72    var_dump(rewind($file_handle));
73    var_dump(ftell($file_handle));
74    var_dump( feof($file_handle) );
75    /*reading 1030 bytes from file, expecting 1024 bytes */ ;
76    echo "Reading 1030 bytes from file, expecting 1024 bytes ... ";
77    $data_from_file=fread($file_handle, 1030);// request for 6 bytes more than its size
78    check_size($data_from_file,1024);
79    var_dump(ftell($file_handle));
80    var_dump( feof($file_handle) );
81    var_dump( md5($data_from_file) ); // calculate the hash and dump it
82
83    // reading 1000 bytes within the file max size
84    var_dump(rewind($file_handle));
85    var_dump(ftell($file_handle));
86    var_dump( feof($file_handle) );
87    /*reading 1000 bytes from file, expecting 1000 bytes */ ;
88    echo "Reading 1000 bytes from file, expecting 1000 bytes ... ";
89    $data_from_file=fread($file_handle, 1000);// request for 24 bytes less than its size
90    check_size($data_from_file,1000);
91    var_dump(ftell($file_handle));
92    var_dump( feof($file_handle) );
93    var_dump( md5($data_from_file) ); // calculate the hash and dump it
94    var_dump(fclose($file_handle)); // now close the file
95  } // end of inner for loop
96
97  // delete the file created
98  delete_file($filename); // delete file with name
99} // end of outer foreach loop
100echo "Done\n";
101?>
102--EXPECT--
103*** Testing fread() basic operations ***
104
105-- Testing fread) with file having data of type numeric --
106-- File opened in mode r --
107int(0)
108bool(false)
109Reading 1024 bytes from file, expecting 1024 bytes ... OK
110int(1024)
111bool(false)
112string(32) "950b7457d1deb6332f2fc5d42f3129d6"
113bool(true)
114int(0)
115bool(false)
116Reading 1030 bytes from file, expecting 1024 bytes ... OK
117int(1024)
118bool(true)
119string(32) "950b7457d1deb6332f2fc5d42f3129d6"
120bool(true)
121int(0)
122bool(false)
123Reading 1000 bytes from file, expecting 1000 bytes ... OK
124int(1000)
125bool(false)
126string(32) "4501f99f2b79d0345f26f1394aca58a3"
127bool(true)
128-- File opened in mode rb --
129int(0)
130bool(false)
131Reading 1024 bytes from file, expecting 1024 bytes ... OK
132int(1024)
133bool(false)
134string(32) "950b7457d1deb6332f2fc5d42f3129d6"
135bool(true)
136int(0)
137bool(false)
138Reading 1030 bytes from file, expecting 1024 bytes ... OK
139int(1024)
140bool(true)
141string(32) "950b7457d1deb6332f2fc5d42f3129d6"
142bool(true)
143int(0)
144bool(false)
145Reading 1000 bytes from file, expecting 1000 bytes ... OK
146int(1000)
147bool(false)
148string(32) "4501f99f2b79d0345f26f1394aca58a3"
149bool(true)
150-- File opened in mode rt --
151int(0)
152bool(false)
153Reading 1024 bytes from file, expecting 1024 bytes ... OK
154int(1024)
155bool(false)
156string(32) "950b7457d1deb6332f2fc5d42f3129d6"
157bool(true)
158int(0)
159bool(false)
160Reading 1030 bytes from file, expecting 1024 bytes ... OK
161int(1024)
162bool(true)
163string(32) "950b7457d1deb6332f2fc5d42f3129d6"
164bool(true)
165int(0)
166bool(false)
167Reading 1000 bytes from file, expecting 1000 bytes ... OK
168int(1000)
169bool(false)
170string(32) "4501f99f2b79d0345f26f1394aca58a3"
171bool(true)
172-- File opened in mode r+ --
173int(0)
174bool(false)
175Reading 1024 bytes from file, expecting 1024 bytes ... OK
176int(1024)
177bool(false)
178string(32) "950b7457d1deb6332f2fc5d42f3129d6"
179bool(true)
180int(0)
181bool(false)
182Reading 1030 bytes from file, expecting 1024 bytes ... OK
183int(1024)
184bool(true)
185string(32) "950b7457d1deb6332f2fc5d42f3129d6"
186bool(true)
187int(0)
188bool(false)
189Reading 1000 bytes from file, expecting 1000 bytes ... OK
190int(1000)
191bool(false)
192string(32) "4501f99f2b79d0345f26f1394aca58a3"
193bool(true)
194-- File opened in mode r+b --
195int(0)
196bool(false)
197Reading 1024 bytes from file, expecting 1024 bytes ... OK
198int(1024)
199bool(false)
200string(32) "950b7457d1deb6332f2fc5d42f3129d6"
201bool(true)
202int(0)
203bool(false)
204Reading 1030 bytes from file, expecting 1024 bytes ... OK
205int(1024)
206bool(true)
207string(32) "950b7457d1deb6332f2fc5d42f3129d6"
208bool(true)
209int(0)
210bool(false)
211Reading 1000 bytes from file, expecting 1000 bytes ... OK
212int(1000)
213bool(false)
214string(32) "4501f99f2b79d0345f26f1394aca58a3"
215bool(true)
216-- File opened in mode r+t --
217int(0)
218bool(false)
219Reading 1024 bytes from file, expecting 1024 bytes ... OK
220int(1024)
221bool(false)
222string(32) "950b7457d1deb6332f2fc5d42f3129d6"
223bool(true)
224int(0)
225bool(false)
226Reading 1030 bytes from file, expecting 1024 bytes ... OK
227int(1024)
228bool(true)
229string(32) "950b7457d1deb6332f2fc5d42f3129d6"
230bool(true)
231int(0)
232bool(false)
233Reading 1000 bytes from file, expecting 1000 bytes ... OK
234int(1000)
235bool(false)
236string(32) "4501f99f2b79d0345f26f1394aca58a3"
237bool(true)
238
239-- Testing fread) with file having data of type text --
240-- File opened in mode r --
241int(0)
242bool(false)
243Reading 1024 bytes from file, expecting 1024 bytes ... OK
244int(1024)
245bool(false)
246string(32) "e486000c4c8452774f746a27658d87fa"
247bool(true)
248int(0)
249bool(false)
250Reading 1030 bytes from file, expecting 1024 bytes ... OK
251int(1024)
252bool(true)
253string(32) "e486000c4c8452774f746a27658d87fa"
254bool(true)
255int(0)
256bool(false)
257Reading 1000 bytes from file, expecting 1000 bytes ... OK
258int(1000)
259bool(false)
260string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd"
261bool(true)
262-- File opened in mode rb --
263int(0)
264bool(false)
265Reading 1024 bytes from file, expecting 1024 bytes ... OK
266int(1024)
267bool(false)
268string(32) "e486000c4c8452774f746a27658d87fa"
269bool(true)
270int(0)
271bool(false)
272Reading 1030 bytes from file, expecting 1024 bytes ... OK
273int(1024)
274bool(true)
275string(32) "e486000c4c8452774f746a27658d87fa"
276bool(true)
277int(0)
278bool(false)
279Reading 1000 bytes from file, expecting 1000 bytes ... OK
280int(1000)
281bool(false)
282string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd"
283bool(true)
284-- File opened in mode rt --
285int(0)
286bool(false)
287Reading 1024 bytes from file, expecting 1024 bytes ... OK
288int(1024)
289bool(false)
290string(32) "e486000c4c8452774f746a27658d87fa"
291bool(true)
292int(0)
293bool(false)
294Reading 1030 bytes from file, expecting 1024 bytes ... OK
295int(1024)
296bool(true)
297string(32) "e486000c4c8452774f746a27658d87fa"
298bool(true)
299int(0)
300bool(false)
301Reading 1000 bytes from file, expecting 1000 bytes ... OK
302int(1000)
303bool(false)
304string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd"
305bool(true)
306-- File opened in mode r+ --
307int(0)
308bool(false)
309Reading 1024 bytes from file, expecting 1024 bytes ... OK
310int(1024)
311bool(false)
312string(32) "e486000c4c8452774f746a27658d87fa"
313bool(true)
314int(0)
315bool(false)
316Reading 1030 bytes from file, expecting 1024 bytes ... OK
317int(1024)
318bool(true)
319string(32) "e486000c4c8452774f746a27658d87fa"
320bool(true)
321int(0)
322bool(false)
323Reading 1000 bytes from file, expecting 1000 bytes ... OK
324int(1000)
325bool(false)
326string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd"
327bool(true)
328-- File opened in mode r+b --
329int(0)
330bool(false)
331Reading 1024 bytes from file, expecting 1024 bytes ... OK
332int(1024)
333bool(false)
334string(32) "e486000c4c8452774f746a27658d87fa"
335bool(true)
336int(0)
337bool(false)
338Reading 1030 bytes from file, expecting 1024 bytes ... OK
339int(1024)
340bool(true)
341string(32) "e486000c4c8452774f746a27658d87fa"
342bool(true)
343int(0)
344bool(false)
345Reading 1000 bytes from file, expecting 1000 bytes ... OK
346int(1000)
347bool(false)
348string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd"
349bool(true)
350-- File opened in mode r+t --
351int(0)
352bool(false)
353Reading 1024 bytes from file, expecting 1024 bytes ... OK
354int(1024)
355bool(false)
356string(32) "e486000c4c8452774f746a27658d87fa"
357bool(true)
358int(0)
359bool(false)
360Reading 1030 bytes from file, expecting 1024 bytes ... OK
361int(1024)
362bool(true)
363string(32) "e486000c4c8452774f746a27658d87fa"
364bool(true)
365int(0)
366bool(false)
367Reading 1000 bytes from file, expecting 1000 bytes ... OK
368int(1000)
369bool(false)
370string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd"
371bool(true)
372
373-- Testing fread) with file having data of type text_with_new_line --
374-- File opened in mode r --
375int(0)
376bool(false)
377Reading 1024 bytes from file, expecting 1024 bytes ... OK
378int(1024)
379bool(false)
380string(32) "b09c8026a64a88d36d4c2f17983964bb"
381bool(true)
382int(0)
383bool(false)
384Reading 1030 bytes from file, expecting 1024 bytes ... OK
385int(1024)
386bool(true)
387string(32) "b09c8026a64a88d36d4c2f17983964bb"
388bool(true)
389int(0)
390bool(false)
391Reading 1000 bytes from file, expecting 1000 bytes ... OK
392int(1000)
393bool(false)
394string(32) "a148fa8110bbac875d84fc9d7056c0a1"
395bool(true)
396-- File opened in mode rb --
397int(0)
398bool(false)
399Reading 1024 bytes from file, expecting 1024 bytes ... OK
400int(1024)
401bool(false)
402string(32) "b09c8026a64a88d36d4c2f17983964bb"
403bool(true)
404int(0)
405bool(false)
406Reading 1030 bytes from file, expecting 1024 bytes ... OK
407int(1024)
408bool(true)
409string(32) "b09c8026a64a88d36d4c2f17983964bb"
410bool(true)
411int(0)
412bool(false)
413Reading 1000 bytes from file, expecting 1000 bytes ... OK
414int(1000)
415bool(false)
416string(32) "a148fa8110bbac875d84fc9d7056c0a1"
417bool(true)
418-- File opened in mode rt --
419int(0)
420bool(false)
421Reading 1024 bytes from file, expecting 1024 bytes ... OK
422int(1024)
423bool(false)
424string(32) "b09c8026a64a88d36d4c2f17983964bb"
425bool(true)
426int(0)
427bool(false)
428Reading 1030 bytes from file, expecting 1024 bytes ... OK
429int(1024)
430bool(true)
431string(32) "b09c8026a64a88d36d4c2f17983964bb"
432bool(true)
433int(0)
434bool(false)
435Reading 1000 bytes from file, expecting 1000 bytes ... OK
436int(1000)
437bool(false)
438string(32) "a148fa8110bbac875d84fc9d7056c0a1"
439bool(true)
440-- File opened in mode r+ --
441int(0)
442bool(false)
443Reading 1024 bytes from file, expecting 1024 bytes ... OK
444int(1024)
445bool(false)
446string(32) "b09c8026a64a88d36d4c2f17983964bb"
447bool(true)
448int(0)
449bool(false)
450Reading 1030 bytes from file, expecting 1024 bytes ... OK
451int(1024)
452bool(true)
453string(32) "b09c8026a64a88d36d4c2f17983964bb"
454bool(true)
455int(0)
456bool(false)
457Reading 1000 bytes from file, expecting 1000 bytes ... OK
458int(1000)
459bool(false)
460string(32) "a148fa8110bbac875d84fc9d7056c0a1"
461bool(true)
462-- File opened in mode r+b --
463int(0)
464bool(false)
465Reading 1024 bytes from file, expecting 1024 bytes ... OK
466int(1024)
467bool(false)
468string(32) "b09c8026a64a88d36d4c2f17983964bb"
469bool(true)
470int(0)
471bool(false)
472Reading 1030 bytes from file, expecting 1024 bytes ... OK
473int(1024)
474bool(true)
475string(32) "b09c8026a64a88d36d4c2f17983964bb"
476bool(true)
477int(0)
478bool(false)
479Reading 1000 bytes from file, expecting 1000 bytes ... OK
480int(1000)
481bool(false)
482string(32) "a148fa8110bbac875d84fc9d7056c0a1"
483bool(true)
484-- File opened in mode r+t --
485int(0)
486bool(false)
487Reading 1024 bytes from file, expecting 1024 bytes ... OK
488int(1024)
489bool(false)
490string(32) "b09c8026a64a88d36d4c2f17983964bb"
491bool(true)
492int(0)
493bool(false)
494Reading 1030 bytes from file, expecting 1024 bytes ... OK
495int(1024)
496bool(true)
497string(32) "b09c8026a64a88d36d4c2f17983964bb"
498bool(true)
499int(0)
500bool(false)
501Reading 1000 bytes from file, expecting 1000 bytes ... OK
502int(1000)
503bool(false)
504string(32) "a148fa8110bbac875d84fc9d7056c0a1"
505bool(true)
506
507-- Testing fread) with file having data of type alphanumeric --
508-- File opened in mode r --
509int(0)
510bool(false)
511Reading 1024 bytes from file, expecting 1024 bytes ... OK
512int(1024)
513bool(false)
514string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
515bool(true)
516int(0)
517bool(false)
518Reading 1030 bytes from file, expecting 1024 bytes ... OK
519int(1024)
520bool(true)
521string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
522bool(true)
523int(0)
524bool(false)
525Reading 1000 bytes from file, expecting 1000 bytes ... OK
526int(1000)
527bool(false)
528string(32) "a49d752f980184c7f44568e930f89c72"
529bool(true)
530-- File opened in mode rb --
531int(0)
532bool(false)
533Reading 1024 bytes from file, expecting 1024 bytes ... OK
534int(1024)
535bool(false)
536string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
537bool(true)
538int(0)
539bool(false)
540Reading 1030 bytes from file, expecting 1024 bytes ... OK
541int(1024)
542bool(true)
543string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
544bool(true)
545int(0)
546bool(false)
547Reading 1000 bytes from file, expecting 1000 bytes ... OK
548int(1000)
549bool(false)
550string(32) "a49d752f980184c7f44568e930f89c72"
551bool(true)
552-- File opened in mode rt --
553int(0)
554bool(false)
555Reading 1024 bytes from file, expecting 1024 bytes ... OK
556int(1024)
557bool(false)
558string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
559bool(true)
560int(0)
561bool(false)
562Reading 1030 bytes from file, expecting 1024 bytes ... OK
563int(1024)
564bool(true)
565string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
566bool(true)
567int(0)
568bool(false)
569Reading 1000 bytes from file, expecting 1000 bytes ... OK
570int(1000)
571bool(false)
572string(32) "a49d752f980184c7f44568e930f89c72"
573bool(true)
574-- File opened in mode r+ --
575int(0)
576bool(false)
577Reading 1024 bytes from file, expecting 1024 bytes ... OK
578int(1024)
579bool(false)
580string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
581bool(true)
582int(0)
583bool(false)
584Reading 1030 bytes from file, expecting 1024 bytes ... OK
585int(1024)
586bool(true)
587string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
588bool(true)
589int(0)
590bool(false)
591Reading 1000 bytes from file, expecting 1000 bytes ... OK
592int(1000)
593bool(false)
594string(32) "a49d752f980184c7f44568e930f89c72"
595bool(true)
596-- File opened in mode r+b --
597int(0)
598bool(false)
599Reading 1024 bytes from file, expecting 1024 bytes ... OK
600int(1024)
601bool(false)
602string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
603bool(true)
604int(0)
605bool(false)
606Reading 1030 bytes from file, expecting 1024 bytes ... OK
607int(1024)
608bool(true)
609string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
610bool(true)
611int(0)
612bool(false)
613Reading 1000 bytes from file, expecting 1000 bytes ... OK
614int(1000)
615bool(false)
616string(32) "a49d752f980184c7f44568e930f89c72"
617bool(true)
618-- File opened in mode r+t --
619int(0)
620bool(false)
621Reading 1024 bytes from file, expecting 1024 bytes ... OK
622int(1024)
623bool(false)
624string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
625bool(true)
626int(0)
627bool(false)
628Reading 1030 bytes from file, expecting 1024 bytes ... OK
629int(1024)
630bool(true)
631string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
632bool(true)
633int(0)
634bool(false)
635Reading 1000 bytes from file, expecting 1000 bytes ... OK
636int(1000)
637bool(false)
638string(32) "a49d752f980184c7f44568e930f89c72"
639bool(true)
640Done
641