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...only valid for Windows');
7}
8?>
9--FILE--
10<?php
11// include the file.inc for common functions for test
12include ("file.inc");
13
14/* Function : function check_read(resource $file_handle, int $read_size, int $expect_size)
15   Description : Read data from file of size $read_size and verifies that $expected_size no. of
16                 bytes are read.
17     $file_handle : File Handle
18     $read_size   : No. of bytes to be read.
19     $expect_size : Expected data length
20   Returns: returns the data read
21*/
22function check_read($file_handle, $read_size, $expect_size) {
23  // print file pointer position before read
24  var_dump( ftell($file_handle) );
25  var_dump( feof($file_handle) );
26
27  // read the data of size $read_size
28  echo "Reading $read_size bytes from file, expecting $expect_size bytes ... ";
29  $data_from_file = fread($file_handle, $read_size);
30
31  // check if data read is of expected size
32  if ( strlen($data_from_file) == $expect_size)
33    echo "OK\n";
34  else
35    echo "Error reading file, total number of bytes read = ".strlen($data_from_file)."\n";
36
37  // file pointer position after read
38  var_dump( ftell($file_handle) );
39  // check if file pointer at eof()
40  var_dump( feof($file_handle) );
41
42  return $data_from_file;
43}
44
45echo "*** Testing fread() : usage variations ***\n";
46
47$file_modes = array("a+","a+b","a+t",
48                    "w+","w+b","w+t",
49                    "x+","x+b","x+t");
50
51$file_content_types = array("numeric","text","text_with_new_line");
52
53foreach($file_content_types as $file_content_type) {
54  echo "\n-- Testing fread() with file having content of type ". $file_content_type ." --\n";
55
56  /* open the file using $files_modes and perform fread() on it */
57  foreach($file_modes as $file_mode) {
58    if(!strstr($file_mode,"x")){
59       /* create files with $file_content_type */
60       create_files ( __DIR__, 1, $file_content_type, 0755, 1, "w", "私はガラスを食べられますfread_variation", 3);
61    }
62
63    $filename = __DIR__."/私はガラスを食べられますfread_variation3.tmp"; // this is name of the file created by create_files()
64    echo "-- File opened in mode ".$file_mode." --\n";
65    $file_handle = fopen($filename, $file_mode);
66    if (!$file_handle) {
67       echo "Error: failed to fopen() file: $filename!";
68       exit();
69    }
70
71    if(strstr($file_mode,"w") || strstr($file_mode,"x") ) {
72      $data_to_be_written="";
73      fill_file($file_handle, $file_content_type, 1024);
74    }
75
76    rewind($file_handle);
77
78    // read file by giving size more than its size
79    echo "-- Reading beyond filesize, expected : 1024 bytes --\n";
80    rewind($file_handle);
81    $data_from_file = check_read($file_handle, 1030, ( strstr($file_mode, "+") ? 1024 : 1024) );
82    if ( $data_from_file != false)
83      var_dump( md5($data_from_file) );
84
85    rewind($file_handle);
86    echo "-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --\n";
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--EXPECT--
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, expected : 1024 bytes --
110int(0)
111bool(false)
112Reading 1030 bytes from file, expecting 1024 bytes ... OK
113int(1024)
114bool(true)
115string(32) "950b7457d1deb6332f2fc5d42f3129d6"
116-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
117int(1024)
118bool(false)
119Reading 10 bytes from file, expecting 0 bytes ... OK
120int(1024)
121bool(true)
122-- File opened in mode a+b --
123-- Reading beyond filesize, expected : 1024 bytes --
124int(0)
125bool(false)
126Reading 1030 bytes from file, expecting 1024 bytes ... OK
127int(1024)
128bool(true)
129string(32) "950b7457d1deb6332f2fc5d42f3129d6"
130-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
131int(1024)
132bool(false)
133Reading 10 bytes from file, expecting 0 bytes ... OK
134int(1024)
135bool(true)
136-- File opened in mode a+t --
137-- Reading beyond filesize, expected : 1024 bytes --
138int(0)
139bool(false)
140Reading 1030 bytes from file, expecting 1024 bytes ... OK
141int(1024)
142bool(true)
143string(32) "950b7457d1deb6332f2fc5d42f3129d6"
144-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
145int(1024)
146bool(false)
147Reading 10 bytes from file, expecting 0 bytes ... OK
148int(1024)
149bool(true)
150-- File opened in mode w+ --
151-- Reading beyond filesize, expected : 1024 bytes --
152int(0)
153bool(false)
154Reading 1030 bytes from file, expecting 1024 bytes ... OK
155int(1024)
156bool(true)
157string(32) "950b7457d1deb6332f2fc5d42f3129d6"
158-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
159int(1024)
160bool(false)
161Reading 10 bytes from file, expecting 0 bytes ... OK
162int(1024)
163bool(true)
164-- File opened in mode w+b --
165-- Reading beyond filesize, expected : 1024 bytes --
166int(0)
167bool(false)
168Reading 1030 bytes from file, expecting 1024 bytes ... OK
169int(1024)
170bool(true)
171string(32) "950b7457d1deb6332f2fc5d42f3129d6"
172-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
173int(1024)
174bool(false)
175Reading 10 bytes from file, expecting 0 bytes ... OK
176int(1024)
177bool(true)
178-- File opened in mode w+t --
179-- Reading beyond filesize, expected : 1024 bytes --
180int(0)
181bool(false)
182Reading 1030 bytes from file, expecting 1024 bytes ... OK
183int(1024)
184bool(true)
185string(32) "950b7457d1deb6332f2fc5d42f3129d6"
186-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
187int(1024)
188bool(false)
189Reading 10 bytes from file, expecting 0 bytes ... OK
190int(1024)
191bool(true)
192-- File opened in mode x+ --
193-- Reading beyond filesize, expected : 1024 bytes --
194int(0)
195bool(false)
196Reading 1030 bytes from file, expecting 1024 bytes ... OK
197int(1024)
198bool(true)
199string(32) "950b7457d1deb6332f2fc5d42f3129d6"
200-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
201int(1024)
202bool(false)
203Reading 10 bytes from file, expecting 0 bytes ... OK
204int(1024)
205bool(true)
206-- File opened in mode x+b --
207-- Reading beyond filesize, expected : 1024 bytes --
208int(0)
209bool(false)
210Reading 1030 bytes from file, expecting 1024 bytes ... OK
211int(1024)
212bool(true)
213string(32) "950b7457d1deb6332f2fc5d42f3129d6"
214-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
215int(1024)
216bool(false)
217Reading 10 bytes from file, expecting 0 bytes ... OK
218int(1024)
219bool(true)
220-- File opened in mode x+t --
221-- Reading beyond filesize, expected : 1024 bytes --
222int(0)
223bool(false)
224Reading 1030 bytes from file, expecting 1024 bytes ... OK
225int(1024)
226bool(true)
227string(32) "950b7457d1deb6332f2fc5d42f3129d6"
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 ... OK
232int(1024)
233bool(true)
234
235-- Testing fread() with file having content of type text --
236-- File opened in mode a+ --
237-- Reading beyond filesize, expected : 1024 bytes --
238int(0)
239bool(false)
240Reading 1030 bytes from file, expecting 1024 bytes ... OK
241int(1024)
242bool(true)
243string(32) "e486000c4c8452774f746a27658d87fa"
244-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
245int(1024)
246bool(false)
247Reading 10 bytes from file, expecting 0 bytes ... OK
248int(1024)
249bool(true)
250-- File opened in mode a+b --
251-- Reading beyond filesize, expected : 1024 bytes --
252int(0)
253bool(false)
254Reading 1030 bytes from file, expecting 1024 bytes ... OK
255int(1024)
256bool(true)
257string(32) "e486000c4c8452774f746a27658d87fa"
258-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
259int(1024)
260bool(false)
261Reading 10 bytes from file, expecting 0 bytes ... OK
262int(1024)
263bool(true)
264-- File opened in mode a+t --
265-- Reading beyond filesize, expected : 1024 bytes --
266int(0)
267bool(false)
268Reading 1030 bytes from file, expecting 1024 bytes ... OK
269int(1024)
270bool(true)
271string(32) "e486000c4c8452774f746a27658d87fa"
272-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
273int(1024)
274bool(false)
275Reading 10 bytes from file, expecting 0 bytes ... OK
276int(1024)
277bool(true)
278-- File opened in mode w+ --
279-- Reading beyond filesize, expected : 1024 bytes --
280int(0)
281bool(false)
282Reading 1030 bytes from file, expecting 1024 bytes ... OK
283int(1024)
284bool(true)
285string(32) "e486000c4c8452774f746a27658d87fa"
286-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
287int(1024)
288bool(false)
289Reading 10 bytes from file, expecting 0 bytes ... OK
290int(1024)
291bool(true)
292-- File opened in mode w+b --
293-- Reading beyond filesize, expected : 1024 bytes --
294int(0)
295bool(false)
296Reading 1030 bytes from file, expecting 1024 bytes ... OK
297int(1024)
298bool(true)
299string(32) "e486000c4c8452774f746a27658d87fa"
300-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
301int(1024)
302bool(false)
303Reading 10 bytes from file, expecting 0 bytes ... OK
304int(1024)
305bool(true)
306-- File opened in mode w+t --
307-- Reading beyond filesize, expected : 1024 bytes --
308int(0)
309bool(false)
310Reading 1030 bytes from file, expecting 1024 bytes ... OK
311int(1024)
312bool(true)
313string(32) "e486000c4c8452774f746a27658d87fa"
314-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
315int(1024)
316bool(false)
317Reading 10 bytes from file, expecting 0 bytes ... OK
318int(1024)
319bool(true)
320-- File opened in mode x+ --
321-- Reading beyond filesize, expected : 1024 bytes --
322int(0)
323bool(false)
324Reading 1030 bytes from file, expecting 1024 bytes ... OK
325int(1024)
326bool(true)
327string(32) "e486000c4c8452774f746a27658d87fa"
328-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
329int(1024)
330bool(false)
331Reading 10 bytes from file, expecting 0 bytes ... OK
332int(1024)
333bool(true)
334-- File opened in mode x+b --
335-- Reading beyond filesize, expected : 1024 bytes --
336int(0)
337bool(false)
338Reading 1030 bytes from file, expecting 1024 bytes ... OK
339int(1024)
340bool(true)
341string(32) "e486000c4c8452774f746a27658d87fa"
342-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
343int(1024)
344bool(false)
345Reading 10 bytes from file, expecting 0 bytes ... OK
346int(1024)
347bool(true)
348-- File opened in mode x+t --
349-- Reading beyond filesize, expected : 1024 bytes --
350int(0)
351bool(false)
352Reading 1030 bytes from file, expecting 1024 bytes ... OK
353int(1024)
354bool(true)
355string(32) "e486000c4c8452774f746a27658d87fa"
356-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
357int(1024)
358bool(false)
359Reading 10 bytes from file, expecting 0 bytes ... OK
360int(1024)
361bool(true)
362
363-- Testing fread() with file having content of type text_with_new_line --
364-- File opened in mode a+ --
365-- Reading beyond filesize, expected : 1024 bytes --
366int(0)
367bool(false)
368Reading 1030 bytes from file, expecting 1024 bytes ... OK
369int(1024)
370bool(true)
371string(32) "b09c8026a64a88d36d4c2f17983964bb"
372-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
373int(1024)
374bool(false)
375Reading 10 bytes from file, expecting 0 bytes ... OK
376int(1024)
377bool(true)
378-- File opened in mode a+b --
379-- Reading beyond filesize, expected : 1024 bytes --
380int(0)
381bool(false)
382Reading 1030 bytes from file, expecting 1024 bytes ... OK
383int(1024)
384bool(true)
385string(32) "b09c8026a64a88d36d4c2f17983964bb"
386-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
387int(1024)
388bool(false)
389Reading 10 bytes from file, expecting 0 bytes ... OK
390int(1024)
391bool(true)
392-- File opened in mode a+t --
393-- Reading beyond filesize, expected : 1024 bytes --
394int(0)
395bool(false)
396Reading 1030 bytes from file, expecting 1024 bytes ... OK
397int(1024)
398bool(true)
399string(32) "b09c8026a64a88d36d4c2f17983964bb"
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 ... OK
404int(1024)
405bool(true)
406-- File opened in mode w+ --
407-- Reading beyond filesize, expected : 1024 bytes --
408int(0)
409bool(false)
410Reading 1030 bytes from file, expecting 1024 bytes ... OK
411int(1024)
412bool(true)
413string(32) "b09c8026a64a88d36d4c2f17983964bb"
414-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
415int(1024)
416bool(false)
417Reading 10 bytes from file, expecting 0 bytes ... OK
418int(1024)
419bool(true)
420-- File opened in mode w+b --
421-- Reading beyond filesize, expected : 1024 bytes --
422int(0)
423bool(false)
424Reading 1030 bytes from file, expecting 1024 bytes ... OK
425int(1024)
426bool(true)
427string(32) "b09c8026a64a88d36d4c2f17983964bb"
428-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
429int(1024)
430bool(false)
431Reading 10 bytes from file, expecting 0 bytes ... OK
432int(1024)
433bool(true)
434-- File opened in mode w+t --
435-- Reading beyond filesize, expected : 1024 bytes --
436int(0)
437bool(false)
438Reading 1030 bytes from file, expecting 1024 bytes ... OK
439int(1024)
440bool(true)
441string(32) "b09c8026a64a88d36d4c2f17983964bb"
442-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
443int(1137)
444bool(false)
445Reading 10 bytes from file, expecting 0 bytes ... OK
446int(1137)
447bool(true)
448-- File opened in mode x+ --
449-- Reading beyond filesize, expected : 1024 bytes --
450int(0)
451bool(false)
452Reading 1030 bytes from file, expecting 1024 bytes ... OK
453int(1024)
454bool(true)
455string(32) "b09c8026a64a88d36d4c2f17983964bb"
456-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
457int(1024)
458bool(false)
459Reading 10 bytes from file, expecting 0 bytes ... OK
460int(1024)
461bool(true)
462-- File opened in mode x+b --
463-- Reading beyond filesize, expected : 1024 bytes --
464int(0)
465bool(false)
466Reading 1030 bytes from file, expecting 1024 bytes ... OK
467int(1024)
468bool(true)
469string(32) "b09c8026a64a88d36d4c2f17983964bb"
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 ... OK
474int(1024)
475bool(true)
476-- File opened in mode x+t --
477-- Reading beyond filesize, expected : 1024 bytes --
478int(0)
479bool(false)
480Reading 1030 bytes from file, expecting 1024 bytes ... OK
481int(1024)
482bool(true)
483string(32) "b09c8026a64a88d36d4c2f17983964bb"
484-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
485int(1137)
486bool(false)
487Reading 10 bytes from file, expecting 0 bytes ... OK
488int(1137)
489bool(true)
490Done
491