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 = 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 .= '../'.$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 60function test_fopen($mode) { 61 global $scriptFile, $secondFile, $firstFile, $filename; 62 63 // create a file in the middle directory 64 $h = fopen($secondFile, "w"); 65 fwrite($h, (binary) "in dir2"); 66 fclose($h); 67 68 echo "\n** testing with mode=$mode **\n"; 69 // should read dir2 file 70 $h = fopen($filename, $mode, true); 71 fpassthru($h); 72 fclose($h); 73 echo "\n"; 74 75 //create a file in dir1 76 $h = fopen($firstFile, "w"); 77 fwrite($h, (binary) "in dir1"); 78 fclose($h); 79 80 //should now read dir1 file 81 $h = fopen($filename, $mode, true); 82 fpassthru($h); 83 fclose($h); 84 echo "\n"; 85 86 // create a file in working directory 87 $h = fopen($filename, "w"); 88 fwrite($h, (binary) "in working dir"); 89 fclose($h); 90 91 //should read the dir1 file 92 $h = fopen($filename, $mode, true); 93 fpassthru($h); 94 fclose($h); 95 echo "\n"; 96 97 unlink($firstFile); 98 unlink($secondFile); 99 100 //should read the working dir file 101 $h = fopen($filename, $mode, true); 102 fpassthru($h); 103 fclose($h); 104 echo "\n"; 105 106 // create a file in the script directory 107 $h = fopen($scriptFile, "w"); 108 fwrite($h, (binary) "in script dir"); 109 fclose($h); 110 111 //should read the file in script dir 112 $h = fopen($filename, $mode, true); 113 fpassthru($h); 114 fclose($h); 115 echo "\n"; 116 117 //cleanup 118 unlink($filename); 119 unlink($scriptFile); 120 121} 122 123?> 124===DONE=== 125--EXPECTF-- 126 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 151