Dir.php
[code]

<?php

/**
 * This class can perform different actions on direcoties, invoking files aswell. Please take a close look at the powerfull callback functions.
 *
 */
class Storeman_Filesystem_Dir
{
	private $_strDirname = '';
	private $_arrDirContent = array();
	private $_iDirsize = 0;
	
	private $_strTargetDir = '';
	
	/**
	 * Constructor
	 *
	 * @param string $strDirname
	 * @return Filesystem_Dir
	 */
	public function __construct($strDirname)
	{
		$this->_strDirname = Storeman_Filesystem_Dir::FilterDirname($strDirname);
		
		if(!is_dir($this->_strDirname)){
			self::CreateDir($this->_strDirname);
		}
	}
	
	static public function FilterDirname( $strDirname ){
		$strDirname = trim($strDirname);
		
		if( substr($strDirname, -1, 1) != '/' ){
			$strDirname .= '/';
		}
		
		return $strDirname;
	}
	
	/**
	 * Create director(y/ies), can create multiple folders in-depth
	 *
	 * @param string $strDirname
	 */
	static public function CreateDir($strDirname)
	{
		$strCurrentDir = '';
		// Replace backslashes by normal slashes
		$strDirname = str_replace('\\', '/',$strDirname);
		
		$arrDir = explode('/', $strDirname);
		
		foreach($arrDir as $strSubdirname)
		{
			$strCurrentDir .= $strSubdirname.'/';
			
			if(!is_dir($strCurrentDir)) mkdir($strCurrentDir, 0777);
		}
		
	}
	
	/**
	 * Get the content of a directory using an associative array. array('folder1'=>array('folder1_1'=>array('file1.php','file2.php'), 'file1.php')
	 *
	 * @param array $arrFolderContent	used
	 * @return unknown
	 */
	public function GetDirContent( $iLevel = -1 )
	{	
		$arrFolderContent = array();
		
		if(($dh = opendir( $this->_strDirname ) ) !== false){
			while(($file = readdir($dh)) !== false){
				
				if($file != '.' && $file != '..'){						
					if(is_dir( $this->_strDirname.$file.'/')){
						
						if(!isset($arrFolderContent[$file]) || !is_array($arrFolderContent[$file])){
							$arrFolderContent[$file] = array();
						}
						
						$oDir = new Storeman_Filesystem_Dir( $this->_strDirname.$file.'/' );
						if( $iLevel > 0 ){
							$arrFolderContent[$file] = $oDir->GetDirContent( $iLevel-1 );
						}elseif( $iLevel === -1 ){
							$arrFolderContent[$file] = $oDir->GetDirContent( $iLevel );
						}
						
					}elseif(is_file( $this->_strDirname.$file)) {
						$arrFolderContent[] = $file;
					}
				}
			}
			
			$this->_arrDirContent = $arrFolderContent;
			return $arrFolderContent;
		
		}else{
			return false;
		}
	}
	
	/**
	 * Using this to perform a function or method on a file. The callback function will get two parameters, the directoryname and the filename. The files and folders will be processed top-down, that is: index.php will be processed before main/index.php.
	 *
	 * @param mixed $callback
	 * @param integer $iLevel, deepness of applying, -1 no limit.
	 * @return true or false
	 */
	public function CallbackFiles( $callback, $iLevel = -1 ){
		
		if( !is_callable($callback) ){
			trigger_error('Calling to undefined function: '.print_r($callback, true), E_USER_WARNING);
			return false;
		}
		
		
		
		if(($dh = opendir( $this->_strDirname ) ) !== false){
			while(($file = readdir($dh)) !== false){
				
				if($file != '.' && $file != '..'){						
					
					if(is_file( $this->_strDirname.$file) ) {
						call_user_func( $callback, $this->_strDirname, $file);
					}
					
				}
			}
			
			rewinddir($dh);
			
			while(($file = readdir($dh)) !== false){
				
				if($file != '.' && $file != '..'){						
					if(is_dir( $this->_strDirname.$file.'/') ){
												
						$oDir = new Storeman_Filesystem_Dir( $this->_strDirname.$file.'/' );
						
						if( $iLevel > 0 ){
							$oDir->CallbackFiles( $callback, $iLevel-1 );
						}elseif( $iLevel === -1 ){
							$oDir->CallbackFiles( $callback, $iLevel );
						}
						
					}
				}
			}
			
			closedir( $dh );
			
			return true;
		
		}else{
			return false;
		}
	}
	
	/**
	 * Using this to perform a function or method on a direcotry. The function will get one parameter, the directoryname . The deepest directory will be done first.
	 *
	 * @param mixed $callback
	 * @param integer $iLevel, deepness of applying, -1 no limit.
	 * @return true or false
	 */
	public function CallbackDirectories( $callback, $iLevel = -1 ){
		if( !is_callable($callback) ){
			trigger_error('Calling to undefined function: '.print_r($callback, true), E_USER_WARNING);
			return false;
		}
		
		if(($dh = opendir( $this->_strDirname ) ) !== false){
			while(($file = readdir($dh)) !== false){
				
				if($file != '.' && $file != '..'){						
					
					if(is_dir( $this->_strDirname.$file.'/')){
												
						$oDir = new Storeman_Filesystem_Dir( $this->_strDirname.$file );
						
						if( $iLevel > 0 ){
							$oDir->CallbackDirectories( $callback, $iLevel-1 );
						}elseif( $iLevel === -1 ){
							$oDir->CallbackDirectories( $callback, $iLevel );
						}
						
					}elseif(is_file( $this->_strDirname.$file)) {
						// Skip file processing
					}
				}
			}
			closedir( $dh );
			
			call_user_func( $callback, $this->_strDirname );
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * Get the total size of a directory, checking all available files
	 *
	 * @return Total directorysize in Bytes
	 */
	public function GetSize( )
	{
		$this->_iDirsize = 0;
		$this->CallbackFiles( array( $this, 'GetFilesize') );
		
		return $this->_iDirsize;
	}
	
	/**
	 * Remove the current directory and all its files
	 *
	 */
	public function Remove()
	{
		$this->CallbackFiles( array( $this, 'RemoveFile') );
		$this->CallbackDirectories( array( $this, 'RemoveDirectory') );
	}
	
	/**
	 * Remove remove all the files and directories from current directory
	 *
	 */
	public function Clean()
	{
		if(($dh = opendir( $this->_strDirname ) ) !== false){
			while(($file = readdir($dh)) !== false){
				
				if($file != '.' && $file != '..'){						
					
					if(is_dir( $this->_strDirname.$file.'/')){
												
						$oDir = new Storeman_Filesystem_Dir( $this->_strDirname.$file );
						$oDir->CallbackFiles( array( $this, 'RemoveFile') );
						$oDir->CallbackDirectories( array( $this, 'RemoveDirectory') );
						
					}elseif(is_file( $this->_strDirname.$file)) {
						$this->CallbackFiles( array( $this, 'RemoveFile') );
					}
				}
			}
			closedir( $dh );
			
			return true;
		}else{
			return false;
		}
		
	}
	
	private function RemoveFile( $strDirname, $strFilename ){
		if ( file_exists( $strDirname.$strFilename )){
			@unlink( $strDirname.$strFilename );
		}
	}
	
	private function RemoveDirectory ( $strDirname ){
		if ( file_exists( $strDirname )){
			@rmdir( $strDirname );
		}
	}
	
	private function GetFilesize(  $strDirname, $strFilename ){
		if( is_file( $strDirname . $strFilename ) ){
			$this->_iDirsize += filesize( $strDirname . $strFilename );
		}
	}
	
	public function Copy( $strTargetDirectory ){
		// Filter the directory and create it
		$this->_strTargetDir = self::FilterDirname( $strTargetDirectory );
		self::CreateDir( $this->_strTargetDir );
		
		$this->CallbackDirectories( array($this, 'CopyDir' ) );
		$this->CallbackFiles( array($this, 'CopyFile' ) );
	}
	
	private function CopyFile( $strDirname, $strFilename ){
		$strCopyToDir = substr_replace( $strDirname, $this->_strTargetDir, 0, strlen($this->_strDirname));
		
		if ( file_exists( $strCopyToDir.$strFilename )){
			@unlink( $strCopyToDir.$strFilename );
		}
		
		copy( $strDirname . $strFilename, $strCopyToDir . $strFilename );
	}
	
	private function CopyDir( $strDirname ){
		$strCopyToDir = substr_replace( $strDirname, $this->_strTargetDir, 0, strlen($this->_strDirname));
		self::CreateDir( $strCopyToDir );
	}
}

[/code]
<?php
voorbeeld.php
[code]
$strDir = 'MyFolder';
$myDir = new Storeman_Filesystem_Dir( $strDir );

// Print alle bestanden in tabel
echo '<table><tr><td>Map</td><td>Bestand</td></tr>';
$myDir->CallbackFiles( 'PrintFiles' );

echo '</table>';

//Callback functie om bestanden te printen, de callback bevat twee parameters
function PrintFiles( $strDirname, $strFilename){
echo '<tr><td>'.$strDirname.'</td><td>'.$strFilename.'</td></tr>' . PHP_EOL;
}
