Location: PHPKode > scripts > Pictoru's File Tree > pictorus-file-tree/PFileTree.inc.php
<?php
/*
 +--------------------------------------------------------------------------+
 | Author			: Ciprian Voicu  						 				|
 | Email			: pictoru at autoportret dot ro			 				| 
 +--------------------------------------------------------------------------+
 | Name				: Pictoru's File Tree								 	|  
 | Filename			: PFileTree.inc.php									 	| 
 | File Type		: Base class										 	|  
 | Description		: Retrieve the list of files and folders and their info	|
 | 					  from a specified location and return it as array 		|
 |				  	  of arrays or as array of objects  	  				|
 | Created Date     : 2007-02-22                  							|
 | Last Modified    : 2007-03-28                  							|
 | Version		    : 2.1		                  							|  
 +--------------------------------------------------------------------------+
 | License 			: Free to use for anyone          						|
 +--------------------------------------------------------------------------+
*/
#
set_time_limit(300);
#
define("PTree_SERVER_NAME"	, $_SERVER['SERVER_NAME']					);
#
define("PTree_HTTP"			, ($_SERVER['HTTPS'])?"https":"http"		);
#
define("PTree_ALLOWED_KEYS"	, 'mtime|mdate|atime|adate|fullpath|dirname|basename|url|size|ext|perms|children|type');
#
class PFileTree{
	#
	function PFileTree($fullpath=NULL, $subdir=NULL){
		if(!defined('PTree_SUBFOLDER') && $subdir!=NULL) define('PTree_SUBFOLDER', $subdir);
		if(!defined('PTree_SITE_PATH') && $fullpath!=NULL) define('PTree_SITE_PATH', $fullpath);
	}
	function Keys(){
		if(defined('PTree_KEYS')){
			$str=preg_replace('/[^a-z]+/', '|', strtolower(PTree_KEYS));
			$arr=explode('|', trim($str, '|'));
			foreach($arr as $key=>$val){
				$all=explode('|', PTree_ALLOWED_KEYS);
				if(!in_array($val, $all)){
					unset($arr[$key]);
				}
			}
		}else{
			$arr=explode('|', PTree_ALLOWED_KEYS);
		}
		if(is_array($arr)) return $arr;
	}
	function ListFiles($dir=NULL, $recursive=true, $object=true, $keys=NULL){
		if($keys!=NULL && !defined('PTree_KEYS')) define('PTree_KEYS', $keys);
		if($dir==NULL){
			$this->PushError("No location has been provided for scaning!");
			exit;
		}
		#
		$dir	=	realpath($dir);
		$list	=	array();
		$d  = opendir($dir);
		while (false !== ($filename = readdir($d))) {
			if($filename!="." && $filename!=".."){
				switch($object){
					case true:
						$list[$filename]=new FileInfo(realpath($dir."/".$filename), $recursive, $object);
						break;
					default:
						$list[$filename]=$this->Info(realpath($dir."/".$filename), $recursive, $object);
						break;
				}
			}
		}
		closedir($d);
		#
		ksort($list);
		#
		return $list;
	}
	#
	# BEST USED AS PRIVATE FUNCTION FROM WITHIN THE CLASS
	function Info($file, $recursive=false, $object=true){
		$keys=$this->Keys();
		$path=pathinfo($file);
		#
		if(in_array('mtime', $keys)) $info["mtime"]=filemtime($file);
		if(in_array('mdate', $keys)) $info["mdate"]=date("Y-m-d H:i:s", $info["mtime"]);
		if(in_array('atime', $keys)) $info["atime"]=fileatime($file);
		if(in_array('adate', $keys)) $info["adate"]=date("Y-m-d H:i:s", $info["atime"]);
		$info['fullpath']=realpath($path['dirname']."/".$path['basename']);
		if(in_array('dirname', $keys)) $info["dirname"]=$path['dirname'];
		if(in_array('basename', $keys)) $info["basename"]=$path['basename'];
		if(in_array('url', $keys)) {
			if(defined('PTree_SITE_PATH')){
				$info["url"]=$this->FileURL($file);
			}
		}
		if(in_array('type', $keys)) $info["type"]=filetype($file);
		if(is_file($file)){
			if(in_array('size', $keys)) $info["size"]=filesize($file);
			if(in_array('ext', $keys)) $info["ext"]=strtolower($path['extension']);
		}
		if(in_array('perms', $keys)) $info["perms"]=substr(sprintf('%o', fileperms($file)), -4);
		if(is_dir($file) && $recursive==true){
			$info["children"]=$this->ListFiles($file, $recursive, $object);
		}
		clearstatcache();
		#
		return $info;
	}
	#
	function PushError($err){
		$str="<div style=\"padding:10px;background:#FFFDE6;border:1px dotted #0000A0;font-family:Verdana;font-size:11px;color:#BF4300;\">";
		$str.="<b>PFileTree:</b> ".$err."</div>";
		print $str;
		exit;
	}
	#
	function FileURL($file){
		$file=str_replace('\\', '/', $file);
		$site_path=str_replace('\\', '/', PTree_SITE_PATH);
		$file=str_replace($site_path, '', $file);
		$url=PTree_HTTP."://".PTree_SERVER_NAME;
		if(defined('PTree_SUBFOLDER')) $url.='/'.PTree_SUBFOLDER;
		$url.=$file;
		return $url;
	}
	#
	function Delete($path){
		if(is_file($path)){
			@unlink($path);
		}elseif(is_dir($path)){
			$o=$this->ListFiles($path, true, true, 'type fullpath');
			foreach($o as $key=>$obj){
				if($obj->type=='file'){
					@unlink($obj->fullpath);
				}elseif($obj->type=='dir'){
					$this->Delete($obj->fullpath);
				}
			}
			@rmdir($path);
		}else{
			return false;
		}
	}
}
#
class FileInfo extends PFileTree{
	var $mtime		=NULL	;
	var $mdate		=NULL	;
	var $atime		=NULL	;
	var $adate		=NULL	;
	var $fullpath	=NULL	;
	var $dirname	=NULL	;
	var $basename	=NULL	;
	var $url		=NULL	;
	var $type		=NULL	;
	var $size		='N/A'	;
	var $ext		='N/A'	;
	var $perms		=NULL	;
	var $children	='N/A'	;
	#
	function FileInfo($file, $recursive, $object){
		$path=pathinfo($file);
		$keys=$this->Keys();
		#
		if(in_array('mtime', $keys)) $this->mtime=filemtime($file);
		else unset($this->mtime);
		if(in_array('mdate', $keys)) $this->mdate=date("Y-m-d H:i:s", filemtime($file));
		else unset($this->mdate);
		if(in_array('atime', $keys)) $this->atime=fileatime($file);
		else unset($this->atime);
		if(in_array('adate', $keys)) $this->adate=date("Y-m-d H:i:s", fileatime($file));
		else unset($this->adate);
		$this->fullpath=realpath($path['dirname']."/".$path['basename']);
		if(in_array('dirname', $keys)) $this->dirname=$path['dirname'];
		else unset($this->dirname);
		if(in_array('basename', $keys)) $this->basename=$path['basename'];
		else unset($this->basename);
		if(in_array('url', $keys)) {
			if(defined('PTree_SITE_PATH')){
				$this->url=$this->FileURL($file);
			}else{
				unset($this->url);
			}
		}else{
			unset($this->url);
		}
		if(in_array('type', $keys)) $this->type=filetype($file);
		else unset($this->type);
		if(is_file($file)){
			if(in_array('size', $keys)) $this->size=filesize($file);
			else unset($this->size);
			if(in_array('ext', $keys)) $this->ext=strtolower($path['extension']);
			else unset($this->ext);
		}elseif(is_dir($file)){
			unset($this->size);
			unset($this->ext);
		}
		if(in_array('perms', $keys)) $this->perms=substr(sprintf('%o', fileperms($file)), -4);
		else unset($this->perms);
		if(is_dir($file) && $recursive){
			$this->children=$this->ListFiles($file, $recursive, $object);
		}else{
			unset($this->children);
		}
		clearstatcache();
	}
	#
}
?>
Return current item: Pictoru's File Tree