1--TEST--
2Test file() function : usage variations
3--FILE--
4<?php
5require(__DIR__ . '/file.inc');
6
7$data_array = array( "Garbage data", "Gar\nba\nge d\nata", "Gar\n\nbage \n\n data" );
8echo "*** Using various flags values with different data in a file\n";
9$count=1;
10$file_path = __DIR__;
11foreach( $data_array as $data ) {
12   echo "--Iteration $count --\n";
13   $fh  = fopen($file_path."/file_variation.tmp", "w");
14   fwrite($fh, $data);
15   var_dump( file($file_path."/file_variation.tmp", FILE_IGNORE_NEW_LINES) );
16   var_dump( file($file_path."/file_variation.tmp", FILE_SKIP_EMPTY_LINES) );
17   $count++;
18   fclose($fh);
19}
20
21echo "*** Testing with variation in use_include_path argument ***\n";
22$file_path1 = __DIR__."/file_variation";
23mkdir($file_path1);
24ini_set( 'include_path',$file_path.'/file_variation' );
25
26file_put_contents( $file_path1."/file1_variation.tmp", "aaaaaaaaaaaaaaabbbbbbbbbbb111111111222222222" );
27var_dump( file("file1_variation.tmp", FILE_USE_INCLUDE_PATH) );
28var_dump( file($file_path1."/file1_variation.tmp", 1) );
29
30echo "*** Using file function to remove line containing a key string ***\n";
31$file_handle = fopen($file_path."/file2_variation.tmp", "w");
32$key = "SEARCH_KEY";
33fwrite( $file_handle,"The key string to be searched is SEARCH_KEY\nLine without key string\nThe key string to be searched is SEARCH_KEY" );
34$out_array = file($file_path."/file2_variation.tmp");
35
36echo "File contents in array form Before replacement of the key\n";
37var_dump( $out_array );
38$file_handle2 = fopen($file_path."/file3_variation.tmp", "w");
39// Loop through file content array
40foreach($out_array as $line) {
41  if( !strstr( $line, $key ) )
42    fputs($file_handle2,$line);
43}
44echo "File contents in array form After replacement of the key\n";
45var_dump( file($file_path."/file3_variation.tmp" ));
46fclose($file_handle);
47fclose($file_handle2);
48
49echo "\n--- Done ---";
50?>
51--CLEAN--
52<?php
53$file_path = __DIR__;
54unlink($file_path."/file_variation.tmp");
55unlink($file_path."/file_variation/file1_variation.tmp");
56unlink($file_path."/file2_variation.tmp");
57unlink($file_path."/file3_variation.tmp");
58rmdir($file_path."/file_variation");
59
60?>
61--EXPECT--
62*** Using various flags values with different data in a file
63--Iteration 1 --
64array(1) {
65  [0]=>
66  string(12) "Garbage data"
67}
68array(1) {
69  [0]=>
70  string(12) "Garbage data"
71}
72--Iteration 2 --
73array(4) {
74  [0]=>
75  string(3) "Gar"
76  [1]=>
77  string(2) "ba"
78  [2]=>
79  string(4) "ge d"
80  [3]=>
81  string(3) "ata"
82}
83array(4) {
84  [0]=>
85  string(4) "Gar
86"
87  [1]=>
88  string(3) "ba
89"
90  [2]=>
91  string(5) "ge d
92"
93  [3]=>
94  string(3) "ata"
95}
96--Iteration 3 --
97array(5) {
98  [0]=>
99  string(3) "Gar"
100  [1]=>
101  string(0) ""
102  [2]=>
103  string(5) "bage "
104  [3]=>
105  string(0) ""
106  [4]=>
107  string(5) " data"
108}
109array(5) {
110  [0]=>
111  string(4) "Gar
112"
113  [1]=>
114  string(1) "
115"
116  [2]=>
117  string(6) "bage
118"
119  [3]=>
120  string(1) "
121"
122  [4]=>
123  string(5) " data"
124}
125*** Testing with variation in use_include_path argument ***
126array(1) {
127  [0]=>
128  string(44) "aaaaaaaaaaaaaaabbbbbbbbbbb111111111222222222"
129}
130array(1) {
131  [0]=>
132  string(44) "aaaaaaaaaaaaaaabbbbbbbbbbb111111111222222222"
133}
134*** Using file function to remove line containing a key string ***
135File contents in array form Before replacement of the key
136array(3) {
137  [0]=>
138  string(44) "The key string to be searched is SEARCH_KEY
139"
140  [1]=>
141  string(24) "Line without key string
142"
143  [2]=>
144  string(43) "The key string to be searched is SEARCH_KEY"
145}
146File contents in array form After replacement of the key
147array(1) {
148  [0]=>
149  string(24) "Line without key string
150"
151}
152
153--- Done ---
154