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