1--TEST--
2Test fopen() function : variation: use include path and stream context (relative directories in path)
3--CREDITS--
4Dave Kelsey <d_kelsey@uk.ibm.com>
5--FILE--
6<?php
7$thisTestDir =  basename(__FILE__, ".php") . ".dir";
8mkdir($thisTestDir);
9chdir($thisTestDir);
10
11//create the include directory structure
12$workingDir = "workdir";
13$filename =  basename(__FILE__, ".php") . ".tmp";
14$scriptDir = __DIR__;
15$baseDir = getcwd();
16$secondFile = $baseDir."/dir2/".$filename;
17$firstFile = "../dir1/".$filename;
18$scriptFile = $scriptDir.'/'.$filename;
19
20$newdirs = array("dir1", "dir2", "dir3");
21$pathSep = ":";
22$newIncludePath = "";
23if(substr(PHP_OS, 0, 3) == 'WIN' ) {
24   $pathSep = ";";
25}
26foreach($newdirs as $newdir) {
27   mkdir($newdir);
28   $newIncludePath .= '../'.$newdir.$pathSep;
29}
30mkdir($workingDir);
31chdir($workingDir);
32
33//define the files to go into these directories, create one in dir2
34echo "\n--- testing include path ---\n";
35set_include_path($newIncludePath);
36$modes = array("r", "r+", "rt");
37foreach($modes as $mode) {
38    test_fopen($mode);
39}
40
41// remove the directory structure
42chdir($baseDir);
43rmdir($workingDir);
44foreach($newdirs as $newdir) {
45   rmdir($newdir);
46}
47
48chdir("..");
49rmdir($thisTestDir);
50
51function test_fopen($mode) {
52   global $scriptFile, $secondFile, $firstFile, $filename;
53
54   // create a file in the middle directory
55   $h = fopen($secondFile, "w");
56   fwrite($h, "in dir2");
57   fclose($h);
58
59   echo "\n** testing with mode=$mode **\n";
60   // should read dir2 file
61   $h = fopen($filename, $mode, true);
62   fpassthru($h);
63   fclose($h);
64   echo "\n";
65
66   //create a file in dir1
67   $h = fopen($firstFile, "w");
68   fwrite($h, "in dir1");
69   fclose($h);
70
71   //should now read dir1 file
72   $h = fopen($filename, $mode, true);
73   fpassthru($h);
74   fclose($h);
75   echo "\n";
76
77   // create a file in working directory
78   $h = fopen($filename, "w");
79   fwrite($h, "in working dir");
80   fclose($h);
81
82   //should read the dir1 file
83   $h = fopen($filename, $mode, true);
84   fpassthru($h);
85   fclose($h);
86   echo "\n";
87
88   unlink($firstFile);
89   unlink($secondFile);
90
91   //should read the working dir file
92   $h = fopen($filename, $mode, true);
93   fpassthru($h);
94   fclose($h);
95   echo "\n";
96
97   // create a file in the script directory
98   $h = fopen($scriptFile, "w");
99   fwrite($h, "in script dir");
100   fclose($h);
101
102   //should read the file in script dir
103   $h = fopen($filename, $mode, true);
104   fpassthru($h);
105   fclose($h);
106   echo "\n";
107
108   //cleanup
109   unlink($filename);
110   unlink($scriptFile);
111
112}
113
114?>
115--EXPECT--
116--- testing include path ---
117
118** testing with mode=r **
119in dir2
120in dir1
121in dir1
122in working dir
123in script dir
124
125** testing with mode=r+ **
126in dir2
127in dir1
128in dir1
129in working dir
130in script dir
131
132** testing with mode=rt **
133in dir2
134in dir1
135in dir1
136in working dir
137in script dir
138