1--TEST--
2Test fwrite() function : usage variations - r, rb & rt modes
3--SKIPIF--
4<?php
5if( substr(PHP_OS, 0, 3) != 'WIN' ) {
6   die('skip...Not valid for Linux');
7}
8?>
9--FILE--
10<?php
11/*
12 Prototype: int fwrite ( resource $handle,string string, [, int $length] );
13 Description: fwrite() writes the contents of string to the file stream pointed to by handle.
14              If the length arquement is given,writing will stop after length bytes have been
15              written or the end of string reached, whichever comes first.
16              fwrite() returns the number of bytes written or FALSE on error
17*/
18
19
20echo "*** Testing fwrite() various  operations ***\n";
21
22// include the file.inc for Function: function delete_file($filename)
23include ("file.inc");
24
25/*
26 Test fwrite with file opened in mode : r,rb,rt
27 File having content of type numeric, text,text_with_new_line & alphanumeric
28*/
29
30$file_modes = array("r","rb","rt");
31$file_content_types = array("numeric","text","text_with_new_line","alphanumeric");
32
33
34foreach($file_content_types as $file_content_type) {
35  echo "\n-- Testing fwrite() with file having content of type ". $file_content_type ." --\n";
36
37  /* open the file using $files_modes and perform fwrite() on it */
38  foreach($file_modes as $file_mode) {
39    echo "-- Opening file in $file_mode --\n";
40
41    // create the temp file with content of type $file_content_type
42    $filename = dirname(__FILE__)."/fwrite_variation1私はガラスを食べられます1.tmp"; // this is name of the file
43    create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 1, "w", "fwrite_variation1私はガラスを食べられます");
44
45    $file_handle = fopen($filename, $file_mode);
46    if(!$file_handle) {
47      echo "Error: failed to fopen() file: $filename!";
48      exit();
49    }
50
51    $data_to_be_written="";
52    fill_buffer($data_to_be_written,$file_content_type,1024);  //get the data of size 1024
53
54    /*  Write the data into the file, verify it by checking the file pointer position, eof position,
55        filesize & by displaying the content */
56
57    var_dump( ftell($file_handle) );  // expected: 0
58    var_dump( fwrite($file_handle, $data_to_be_written ));
59    var_dump( ftell($file_handle) );  // expected: 0
60    var_dump( feof($file_handle) );  // expected: false
61
62    // move the file pointer to end of the file and try fwrite()
63    fseek($file_handle, SEEK_END, 0);
64    var_dump( ftell($file_handle) );  // expecting 1024
65    var_dump( fwrite($file_handle, $data_to_be_written) ); // fwrite to fail
66    var_dump( ftell($file_handle) );  //check that file pointer points at eof, expected: 1024
67    var_dump( feof($file_handle) );  // ensure that  feof() points to eof, expected: true
68
69    // ensure that file content/size didn't change.
70    var_dump( fclose($file_handle) );
71    clearstatcache();//clears file status cache
72    var_dump( filesize($filename) );  // expected: 1024
73    var_dump(md5(file_get_contents($filename))); // hash the output
74    delete_file($filename); // delete file with name fwrite_variation1.tmp
75
76  } // end of inner foreach loop
77} // end of outer foreach loop
78
79echo "Done\n";
80?>
81--EXPECTF--
82*** Testing fwrite() various  operations ***
83
84-- Testing fwrite() with file having content of type numeric --
85-- Opening file in r --
86int(0)
87int(0)
88int(0)
89bool(false)
90int(2)
91int(0)
92int(2)
93bool(false)
94bool(true)
95int(1024)
96string(32) "950b7457d1deb6332f2fc5d42f3129d6"
97-- Opening file in rb --
98int(0)
99int(0)
100int(0)
101bool(false)
102int(2)
103int(0)
104int(2)
105bool(false)
106bool(true)
107int(1024)
108string(32) "950b7457d1deb6332f2fc5d42f3129d6"
109-- Opening file in rt --
110int(0)
111int(0)
112int(0)
113bool(false)
114int(2)
115int(0)
116int(2)
117bool(false)
118bool(true)
119int(1024)
120string(32) "950b7457d1deb6332f2fc5d42f3129d6"
121
122-- Testing fwrite() with file having content of type text --
123-- Opening file in r --
124int(0)
125int(0)
126int(0)
127bool(false)
128int(2)
129int(0)
130int(2)
131bool(false)
132bool(true)
133int(1024)
134string(32) "e486000c4c8452774f746a27658d87fa"
135-- Opening file in rb --
136int(0)
137int(0)
138int(0)
139bool(false)
140int(2)
141int(0)
142int(2)
143bool(false)
144bool(true)
145int(1024)
146string(32) "e486000c4c8452774f746a27658d87fa"
147-- Opening file in rt --
148int(0)
149int(0)
150int(0)
151bool(false)
152int(2)
153int(0)
154int(2)
155bool(false)
156bool(true)
157int(1024)
158string(32) "e486000c4c8452774f746a27658d87fa"
159
160-- Testing fwrite() with file having content of type text_with_new_line --
161-- Opening file in r --
162int(0)
163int(0)
164int(0)
165bool(false)
166int(2)
167int(0)
168int(2)
169bool(false)
170bool(true)
171int(1024)
172string(32) "b09c8026a64a88d36d4c2f17983964bb"
173-- Opening file in rb --
174int(0)
175int(0)
176int(0)
177bool(false)
178int(2)
179int(0)
180int(2)
181bool(false)
182bool(true)
183int(1024)
184string(32) "b09c8026a64a88d36d4c2f17983964bb"
185-- Opening file in rt --
186int(0)
187int(0)
188int(0)
189bool(false)
190int(2)
191int(0)
192int(2)
193bool(false)
194bool(true)
195int(1024)
196string(32) "b09c8026a64a88d36d4c2f17983964bb"
197
198-- Testing fwrite() with file having content of type alphanumeric --
199-- Opening file in r --
200int(0)
201int(0)
202int(0)
203bool(false)
204int(2)
205int(0)
206int(2)
207bool(false)
208bool(true)
209int(1024)
210string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
211-- Opening file in rb --
212int(0)
213int(0)
214int(0)
215bool(false)
216int(2)
217int(0)
218int(2)
219bool(false)
220bool(true)
221int(1024)
222string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
223-- Opening file in rt --
224int(0)
225int(0)
226int(0)
227bool(false)
228int(2)
229int(0)
230int(2)
231bool(false)
232bool(true)
233int(1024)
234string(32) "3fabd48d8eaa65c14e0d93d6880c560c"
235Done
236