1--TEST-- 2Test fopen() function : variation: use include path and stream context (absolute 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//create the include directory structure 16$thisTestDir = basename(__FILE__, ".php") . ".dir"; 17mkdir($thisTestDir); 18chdir($thisTestDir); 19 20$workingDir = "workdir"; 21$filename = basename(__FILE__, ".php") . ".tmp"; 22$scriptDir = dirname(__FILE__); 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 .= $baseDir.'/'.$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} 48restore_include_path(); 49 50// remove the directory structure 51chdir($baseDir); 52rmdir($workingDir); 53foreach($newdirs as $newdir) { 54 rmdir($newdir); 55} 56 57chdir(".."); 58rmdir($thisTestDir); 59 60 61function test_fopen($mode) { 62 global $scriptFile, $secondFile, $firstFile, $filename; 63 64 // create a file in the middle directory 65 $h = fopen($secondFile, "w"); 66 fwrite($h, "in dir2"); 67 fclose($h); 68 69 echo "\n** testing with mode=$mode **\n"; 70 // should read dir2 file 71 $h = fopen($filename, $mode, true); 72 fpassthru($h); 73 fclose($h); 74 echo "\n"; 75 76 //create a file in dir1 77 $h = fopen($firstFile, "w"); 78 fwrite($h, "in dir1"); 79 fclose($h); 80 81 //should now read dir1 file 82 $h = fopen($filename, $mode, true); 83 fpassthru($h); 84 fclose($h); 85 echo "\n"; 86 87 // create a file in working directory 88 $h = fopen($filename, "w"); 89 fwrite($h, "in working dir"); 90 fclose($h); 91 92 //should still read dir1 file 93 $h = fopen($filename, $mode, true); 94 fpassthru($h); 95 fclose($h); 96 echo "\n"; 97 98 unlink($firstFile); 99 unlink($secondFile); 100 101 //should read the file in working dir 102 $h = fopen($filename, $mode, true); 103 fpassthru($h); 104 fclose($h); 105 echo "\n"; 106 107 // create a file in the script directory 108 $h = fopen($scriptFile, "w"); 109 fwrite($h, "in script dir"); 110 fclose($h); 111 112 //should read the file in script dir 113 $h = fopen($filename, $mode, true); 114 fpassthru($h); 115 fclose($h); 116 echo "\n"; 117 118 //cleanup 119 unlink($filename); 120 unlink($scriptFile); 121 122} 123 124?> 125===DONE=== 126--EXPECTF-- 127--- testing include path --- 128 129** testing with mode=r ** 130in dir2 131in dir1 132in dir1 133in working dir 134in script dir 135 136** testing with mode=r+ ** 137in dir2 138in dir1 139in dir1 140in working dir 141in script dir 142 143** testing with mode=rt ** 144in dir2 145in dir1 146in dir1 147in working dir 148in script dir 149===DONE=== 150