1--TEST-- 2Test dir() function : usage variations - different directory permissions 3--SKIPIF-- 4<?php 5if( substr(PHP_OS, 0, 3) == 'WIN') { 6 die('skip Not for Windows'); 7} 8// Skip if being run by root (files are always readable, writeable and executable) 9$filename = dirname(__FILE__)."/dir_root_check.tmp"; 10$fp = fopen($filename, 'w'); 11fclose($fp); 12if(fileowner($filename) == 0) { 13 unlink ($filename); 14 die('skip...cannot be run as root\n'); 15} 16unlink($filename); 17?> 18--FILE-- 19<?php 20/* 21 * Prototype : object dir(string $directory[, resource $context]) 22 * Description: Directory class with properties, handle and class and methods read, rewind and close 23 * Source code: ext/standard/dir.c 24 */ 25 26/* 27 * Providing various permissions to the directory to be opened and checking 28 * to see if dir() function opens the directory successfully. 29 */ 30 31echo "*** Testing dir() : different directory permissions ***"; 32 33// create the temporary directory 34$file_path = dirname(__FILE__); 35$dir_path = $file_path."/dir_variation3"; 36@mkdir($dir_path); 37 38/* different values for directory permissions */ 39$permission_values = array( 40/*1*/ 0477, // owner has read only, other and group has rwx 41 0677, // owner has rw only, other and group has rwx 42 43/*3*/ 0444, // all have read only 44 0666, // all have rw only 45 46/*5*/ 0400, // owner has read only, group and others have no permission 47 0600, // owner has rw only, group and others have no permission 48 49/*7*/ 0470, // owner has read only, group has rwx & others have no permission 50 0407, // owner has read only, other has rwx & group has no permission 51 52/*9*/ 0670, // owner has rw only, group has rwx & others have no permission 53/*10*/ 0607 // owner has rw only, group has no permission and others have rwx 54); 55 56// Open directory with different permission values, read and close, expected: none of them to succeed. 57for($count = 0; $count < count($permission_values); $count++) { 58 echo "\n-- Iteration ".($count + 1)." --\n"; 59 60 // try to remove the dir if exists & create 61 $file_path = dirname(__FILE__); 62 $dir_path = $file_path."/dir_variation3"; 63 @chmod ($dir_path, 0777); // change dir permission to allow all operation 64 @rmdir ($dir_path); // try n delete the dir 65 66 // create the dir now 67 @mkdir($dir_path); 68 69 // change the dir permisson to test dir on it 70 var_dump( chmod($dir_path, $permission_values[$count]) ); 71 72 // try to get dir handle 73 $d = dir($dir_path); 74 var_dump($d); // dump the handle 75 76 // try read directory, expected : false 77 echo "-- reading contents --\n"; 78 var_dump($d->read()); 79 80 // close directory 81 $d->close(); 82} 83 84echo "Done"; 85?> 86--CLEAN-- 87<?php 88// deleting temporary directory 89$file_path = dirname(__FILE__); 90$dir_path = $file_path."/dir_variation3"; 91rmdir($dir_path); 92?> 93--EXPECTF-- 94*** Testing dir() : different directory permissions *** 95-- Iteration 1 -- 96bool(true) 97object(Directory)#%d (2) { 98 ["path"]=> 99 string(%d) "%s/dir_variation3" 100 ["handle"]=> 101 resource(%d) of type (stream) 102} 103-- reading contents -- 104string(%d) "%s" 105 106-- Iteration 2 -- 107bool(true) 108object(Directory)#%d (2) { 109 ["path"]=> 110 string(%d) "%s/dir_variation3" 111 ["handle"]=> 112 resource(%d) of type (stream) 113} 114-- reading contents -- 115string(%d) "%s" 116 117-- Iteration 3 -- 118bool(true) 119object(Directory)#%d (2) { 120 ["path"]=> 121 string(%d) "%s/dir_variation3" 122 ["handle"]=> 123 resource(%d) of type (stream) 124} 125-- reading contents -- 126string(%d) "%s" 127 128-- Iteration 4 -- 129bool(true) 130object(Directory)#%d (2) { 131 ["path"]=> 132 string(%d) "%s/dir_variation3" 133 ["handle"]=> 134 resource(%d) of type (stream) 135} 136-- reading contents -- 137string(%d) "%s" 138 139-- Iteration 5 -- 140bool(true) 141object(Directory)#%d (2) { 142 ["path"]=> 143 string(%d) "%s/dir_variation3" 144 ["handle"]=> 145 resource(%d) of type (stream) 146} 147-- reading contents -- 148string(%d) "%s" 149 150-- Iteration 6 -- 151bool(true) 152object(Directory)#%d (2) { 153 ["path"]=> 154 string(%d) "%s/dir_variation3" 155 ["handle"]=> 156 resource(%d) of type (stream) 157} 158-- reading contents -- 159string(%d) "%s" 160 161-- Iteration 7 -- 162bool(true) 163object(Directory)#%d (2) { 164 ["path"]=> 165 string(%d) "%s/dir_variation3" 166 ["handle"]=> 167 resource(%d) of type (stream) 168} 169-- reading contents -- 170string(%d) "%s" 171 172-- Iteration 8 -- 173bool(true) 174object(Directory)#%d (2) { 175 ["path"]=> 176 string(%d) "%s/dir_variation3" 177 ["handle"]=> 178 resource(%d) of type (stream) 179} 180-- reading contents -- 181string(%d) "%s" 182 183-- Iteration 9 -- 184bool(true) 185object(Directory)#%d (2) { 186 ["path"]=> 187 string(%d) "%s/dir_variation3" 188 ["handle"]=> 189 resource(%d) of type (stream) 190} 191-- reading contents -- 192string(%d) "%s" 193 194-- Iteration 10 -- 195bool(true) 196object(Directory)#%d (2) { 197 ["path"]=> 198 string(%d) "%s/dir_variation3" 199 ["handle"]=> 200 resource(%d) of type (stream) 201} 202-- reading contents -- 203string(%d) "%s" 204Done 205