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, $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,"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--CLEAN--
57<?php
58$file_path = dirname(__FILE__);
59unlink($file_path."/file_variation.tmp");
60unlink($file_path."/file_variation/file1_variation.tmp");
61unlink($file_path."/file2_variation.tmp");
62unlink($file_path."/file3_variation.tmp");
63rmdir($file_path."/file_variation");
64
65?>
66--EXPECTF--
67*** Using various flags values with different data in a file
68--Iteration 1 --
69array(1) {
70  [0]=>
71  string(12) "Garbage data"
72}
73array(1) {
74  [0]=>
75  string(12) "Garbage data"
76}
77--Iteration 2 --
78array(4) {
79  [0]=>
80  string(3) "Gar"
81  [1]=>
82  string(2) "ba"
83  [2]=>
84  string(4) "ge d"
85  [3]=>
86  string(3) "ata"
87}
88array(4) {
89  [0]=>
90  string(4) "Gar
91"
92  [1]=>
93  string(3) "ba
94"
95  [2]=>
96  string(5) "ge d
97"
98  [3]=>
99  string(3) "ata"
100}
101--Iteration 3 --
102array(5) {
103  [0]=>
104  string(3) "Gar"
105  [1]=>
106  string(0) ""
107  [2]=>
108  string(5) "bage "
109  [3]=>
110  string(0) ""
111  [4]=>
112  string(5) " data"
113}
114array(5) {
115  [0]=>
116  string(4) "Gar
117"
118  [1]=>
119  string(1) "
120"
121  [2]=>
122  string(6) "bage
123"
124  [3]=>
125  string(1) "
126"
127  [4]=>
128  string(5) " data"
129}
130*** Testing with variation in use_include_path argument ***
131array(1) {
132  [0]=>
133  string(44) "aaaaaaaaaaaaaaabbbbbbbbbbb111111111222222222"
134}
135array(1) {
136  [0]=>
137  string(44) "aaaaaaaaaaaaaaabbbbbbbbbbb111111111222222222"
138}
139*** Using file function to remove line containing a key string ***
140File contents in array form Before replacement of the key
141array(3) {
142  [0]=>
143  string(44) "The key string to be searched is SEARCH_KEY
144"
145  [1]=>
146  string(24) "Line without key string
147"
148  [2]=>
149  string(43) "The key string to be searched is SEARCH_KEY"
150}
151File contents in array form After replacement of the key
152array(1) {
153  [0]=>
154  string(24) "Line without key string
155"
156}
157
158--- Done ---
159