Directory Listing Class
Item 2 of 9
1 <?php 2 3 /** 4 * Provides an interface to access a directory listing and access information about the files 5 * in that directory 6 * 7 * @author Dan Pupius 8 * @version 1.0 9 * @copyright ©2004 pupius.co.uk <http://pupius.co.uk> 10 * @package Utility 11 * @subpackage Classes 12 */ 13 class DirectoryListing { 14 15 16 /** 17 * @var string The path that the current directory listing represents 18 */ 19 var $path = ""; 20 21 22 /** 23 * @var array Array of directory objects (note not a recursive directory listing). Includes names and minimal information 24 */ 25 var $directories = array(); 26 27 /** 28 * @var array Array of file information bjects 29 */ 30 var $files = array(); 31 32 33 34 /** 35 * Constructor function, sets up the object based on the path 36 * 37 * @param string $path Path of directory to open 38 * @access public 39 * @return boolean Whether or not directory list was successfully generated 40 */ 41 function DirectoryListing($path) { 42 if(!is_dir($path)) return false; 43 44 $path = rtrim($path,"/"); 45 46 $this->path = $path; 47 48 //loop through contents of directory and construct arrays of files and directories 49 if($handle = opendir($path)) { 50 51 //loop through each file in the directory and get information on it (not all of it will be needed, but it's added just in case) 52 while (false !== ($f = readdir($handle))) { 53 //directory listing only ignores . and .. files, whatever uses this class should handle security of hidden files 54 if($f != "." && $f != "..") { 55 //get the full path for current file/dir 56 $fullpath = "$path/$f"; 57 58 //if its a directory then push a directory object onto the array 59 if(is_dir($fullpath)) array_push($this->directories,new DirectoryInfo($fullpath)); 60 61 //if its a file then push a file object onto the array 62 else if(is_file($fullpath)) array_push($this->files,new FileInfo($fullpath)); 63 } 64 } 65 closedir($handle); 66 67 return true; 68 69 } else return false; 70 } 71 72 /** 73 * Returns the number of files in the current directory 74 * 75 * @return integer Number of files in current directory 76 * @access public 77 */ 78 function num_files() { 79 return count($this->files); 80 } 81 82 /** 83 * Returns the number of directories in the current directory 84 * 85 * @return integer Number of directories in current directory 86 * @access public 87 */ 88 function num_directories() { 89 return count($this->directories); 90 } 91 92 93 /** 94 * Sort the file array 95 * 96 * @param boolean $asc Whether to sort ascending or descending 97 * @param string $field What property of the FileInfo object to use as the sortable field 98 * @param boolean $natlang Whether to use a natural language based sort 99 * @access public 100 */ 101 function sort_files($asc=true,$field="filename",$natlang=true) { 102 $GLOBALS["file_sort_asc"] = $asc; 103 $GLOBALS["file_sort_field"] = $field; 104 $GLOBALS["file_sort_natlang"] = $natlang; 105 usort($this->files,array("DirectoryListing","filesort")); 106 } 107 108 109 /** 110 * Sort the directory array 111 * 112 * @param boolean $asc Whether to sort ascending or descending 113 * @param string $field What property of the DirectoryInfo object to use as the sortable field 114 * @param boolean $natlang Whether to use a natural language based sort 115 * @access public 116 */ 117 function sort_directories($asc=true,$field="dirname",$natlang=true) { 118 $GLOBALS["file_sort_asc"] = $asc; 119 $GLOBALS["file_sort_field"] = $field; 120 $GLOBALS["file_sort_natlang"] = $natlang; 121 usort($this->directories,array("DirectoryListing","filesort")); 122 } 123 124 125 126 /** 127 * File sorting function 128 * 129 * @param mixes $a First item to compare 130 * @param mixes $b Second item to compare 131 * @return integer 132 * @access public 133 */ 134 function filesort($a,$b) { 135 $field = $GLOBALS["file_sort_field"]; 136 $asc = $GLOBALS["file_sort_asc"]; 137 $natlang = $GLOBALS["file_sort_natlang"]; 138 139 $item1 = strtolower($a->$field); 140 $item2 = strtolower($b->$field); 141 142 if($natlang) $retval = strnatcmp($item1,$item2); 143 else if($item1==$item2) $retval = 0; 144 else $retval = ($item1 < $item2) ? -1 : 1; 145 146 if($asc || $retval==0) return $retval; 147 else if($retval==1) return -1; 148 else return 1; 149 } 150 151 } 152 153 154 /** 155 * Creates an object with easy to access information about a partcular file 156 * 157 * @author Dan Pupius 158 * @version 1.0 159 * @copyright ©2004 pupius.co.uk <http://pupius.co.uk> 160 * @package Utility 161 * @subpackage Classes 162 */ 163 class FileInfo { 164 165 166 /** 167 * @var string The path that the current file object represents 168 */ 169 var $path = ""; 170 171 172 /** 173 * @var string The full path of the file 174 */ 175 var $realpath = ""; 176 177 /** 178 * @var string Filename, without extension 179 */ 180 var $filename = ""; 181 182 /** 183 * @var string File's extension 184 */ 185 var $ext = ""; 186 187 /** 188 * @var string Directory location of the file 189 */ 190 var $dir = ""; 191 192 /** 193 * @var string Full directory path of the file 194 */ 195 var $realdir = ""; 196 197 /** 198 * @var integer Raw size of the file 199 */ 200 var $size = ""; 201 202 /** 203 * @var integer Raw timestamp of last modified date 204 */ 205 var $modified = ""; 206 207 208 /** 209 * Constructor function, sets up the object based on the path 210 * 211 * @param string $path Path of file to open 212 * @access public 213 * @return boolean Whether or not file object was successfully generated 214 */ 215 function FileInfo($path) { 216 if(!file_exists($path) || !is_file($path)) return false; 217 218 $this->path = $path; 219 $this->realpath = realpath($path); 220 221 $pathinfo = pathinfo($path); 222 $this->ext = $pathinfo["extension"]; 223 $this->dir = $pathinfo["dirname"]; 224 $this->realdir = realpath($pathinfo["dirname"]); 225 $this->filename = str_replace(".".$this->ext,"",$pathinfo["basename"]); 226 $this->size = filesize($path); 227 $this->modified = filemtime($path); 228 229 return true; 230 } 231 } 232 233 234 /** 235 * Creates an object with easy to access information about a partcular directory 236 * 237 * @author Dan Pupius 238 * @version 1.0 239 * @copyright ©2004 pupius.co.uk <http://pupius.co.uk> 240 * @package Utility 241 * @subpackage Classes 242 */ 243 class DirectoryInfo { 244 245 246 /** 247 * @var string The path that the current file object represents 248 */ 249 var $path = ""; 250 251 252 /** 253 * @var string The full path of the file 254 */ 255 var $realpath = ""; 256 257 /** 258 * @var string Direcotry name 259 */ 260 var $dirname = ""; 261 262 263 /** 264 * Constructor function, sets up the object based on the path 265 * 266 * @param string $path Path of file to open 267 * @access public 268 * @return boolean Whether or not file object was successfully generated 269 */ 270 function DirectoryInfo($path) { 271 if(!file_exists($path) || !is_dir($path)) return false; 272 273 $this->path = $path; 274 $this->realpath = realpath($path); 275 276 $p = explode("/",$path); 277 $this->dirname = array_pop($p); 278 279 return true; 280 } 281 } 282 283 ?> 284