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