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