import zend standard tests
[hiphop-php.git] / hphp / test / zend / bad / ext-standard-file / fread_variation1.php
blob0d9b077397f460b832ee5c6dbca4d2b2548aee3e
1 <?php
2 /*
3 Prototype: string fread ( resource $handle [, int $length] );
4 Description: reads up to length bytes from the file pointer referenced by handle.
5 Reading stops when up to length bytes have been read, EOF (end of file) is
6 reached, (for network streams) when a packet becomes available, or (after
7 opening userspace stream) when 8192 bytes have been read whichever comes first.
8 */
10 /* Read content less than file size &
11 Read entire file
14 // include the file.inc for common functions for test
15 include ("file.inc");
17 /* Function : function check_read(resource $file_handle, int $read_size, int $expect_size)
18 Description : Read data from file of size $read_size and verifies that $expected_size no. of
19 bytes are read.
20 $file_handle : File Handle
21 $read_size : No. of bytes to be read.
22 $expect_size : Expected data length
23 Returns: returns the data read
25 function check_read($file_handle, $read_size, $expect_size) {
26 // print file pointer position before read
27 var_dump( ftell($file_handle) );
28 var_dump( feof($file_handle) );
30 // read the data of size $read_size
31 echo "Reading $read_size bytes from file, expecting $expect_size bytes ... ";
32 $data_from_file = fread($file_handle, $read_size);
34 // check if data read is of expected size
35 if ( strlen($data_from_file) == $expect_size)
36 echo "OK\n";
37 else
38 echo "Error reading file, total number of bytes read = ".strlen($data_from_file)."\n";
40 // file pointer position after read
41 var_dump( ftell($file_handle) );
42 // check if file pointer at eof()
43 var_dump( feof($file_handle) );
45 return $data_from_file;
48 echo "*** Testing fread() : usage variations ***\n";
50 $file_modes = array("a+","a+b","a+t",
51 "w+","w+b","w+t",
52 "x+","x+b","x+t");
54 $file_content_types = array("numeric","text","text_with_new_line", "alphanumeric");
56 foreach($file_content_types as $file_content_type) {
57 echo "\n-- Testing fread() with file having content of type ". $file_content_type ." --\n";
59 /* open the file using $files_modes and perform fread() on it */
60 foreach($file_modes as $file_mode) {
61 if(!strstr($file_mode,"x")){
62 /* create files with $file_content_type */
63 create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 1, "w", "fread_variation");
66 $filename = dirname(__FILE__)."/fread_variation1.tmp"; // this is name of the file created by create_files()
67 echo "-- File opened in mode ".$file_mode." --\n";
68 $file_handle = fopen($filename, $file_mode);
69 if (!$file_handle) {
70 echo "Error: failed to fopen() file: $filename!";
71 exit();
74 if(strstr($file_mode,"w") || strstr($file_mode,"x") ) {
75 fill_file($file_handle, $file_content_type, 1024);
78 rewind($file_handle);
79 echo "-- Reading entire file content, expeceted : 1024 bytes --\n";
80 // read from file, by giving the file actual size,
81 $data_from_file = check_read($file_handle, 1024, (strstr($file_mode, "+") ? 1024 : 1024 ) );
82 // calculate the hash and dump it, if data read, expecting here no data was read
83 if ( $data_from_file != false)
84 var_dump( md5($data_from_file) );
86 // reading file by giving less than its size
87 echo "-- Reading file content less than max. file size, expeceted : 1000 bytes --\n";
88 rewind($file_handle);
89 $data_from_file = check_read($file_handle, 1000, (strstr($file_mode, "+") ? 1000 : 1000 ) );
90 // calculate the hash and dump it, if data read, expecting here no data was read
91 if ( $data_from_file != false)
92 var_dump( md5($data_from_file) );
94 // now close the file
95 fclose($file_handle);
97 // delete the file created
98 delete_file($filename); // delete file
99 } // end of inner foreach loop
100 }// end of outer foreach loop
102 echo"Done\n";