Scripts

Script die bestanden van map(pen) laat zien

Dit script laat al je bestanden zien in een map (ook submappen) Dit script is makkelijk voor bijv. een gastenboek waar mensen avatars uit mappen kunnen kiezen. Het script is voorzien van commentaar en is makkelijk in gebruik Ik ben niet de eigenaar, lees de comentaars door en je komt er wel achter.

Succes!.class.php
 <?PHP
// Abstracte klasse voor een file of een map
abstract class FileNode{
    protected $name;
    protected $parentDir = null;

    public function __construct($name){
        $this->name = $name;
    }

    public function getName(){
        return $this->name;
    }
	

    public function getPath(){
        $path = '';
        // Als er een map boven zit, stop deze dan voor het pad
        if(!is_null($this->parentDir))
            $path .= $this->parentDir->getPath().'/';

        $path .= $this->name;
    }

    public function setParentDir(Dir $dir){
        $this->parentDir = $dir;
    }
}

class File extends FileNode{
    protected $ext;

    public function __construct($name){
        $this->ext = substr($name, strrpos($name, '.'));
        parent::__construct($name);
    }

    public function getExtension(){
        return $this->ext;
    }
}

class Dir extends FileNode implements IteratorAggregate{
    protected $dirchilds = array();
    protected $filechilds = array();
    protected $name;
    
    public function __construct($name){
        if(strrpos($name,'/')!==false){
            $this->name=substr($name,strrpos($name,'/')+1);
        }else{
            $this->name=$name;
        }
    }

    public function addChild(FileNode $node){
        $node->setParentDir($this);
        if($node instanceof Dir){
            $this->dirchilds[] = $node;
        }elseif($node instanceof File){
            $this->filechilds[] = $node;
        }else{
            throw new Exception('Wrong instance given to Dir::addChild()');
        }
    }

    public function getChilds(){
        return $this->childs;
    }

    public function getIterator(){
        return new ArrayIterator($this->childs);
    }
    
    public function getName(){
        return $this->name;
    }

    public function buildArray($asc=1){
        $array = array();
		
        if(count($this->dirchilds)==0&&count($this->filechilds)==0){
            return 'No files found';
        }else{
            if($asc==1){
                sort($this->dirchilds);
                sort($this->filechilds);
                
                foreach($this->dirchilds as $child)
                    $array[$child->getName()] = $child->buildArray();
                
                foreach($this->filechilds as $child)
                    $array[] = $child->getName();
            }else{
                rsort($this->dirchilds);
                rsort($this->filechilds);
                
                foreach($this->filechilds as $child)
                    $array[] = $child->getName();
                    
                foreach($this->dirchilds as $child)
                    $array[$child->getName()] = $child->buildArray();
            }
            return $array;
        }
    }
}

class FileNodeBuilder{
    private $_dir;
    
    public function build($path,$recursive=true){

        if(!is_dir($path))
            throw new Exception($path.' is no directory');

        if(!is_readable($path))
            throw new Exception($path.' is not readable');

        $dir = new Dir($path);

        foreach(scandir($path,1) as $file){
            $newPath = $path.'/'.$file;
            if(!($file==='.'||$file==='..')){
                if(is_dir($newPath)&&$recursive)
                    $dir->addChild($this->build($newPath));
                if(is_dir($newPath)&&(!$recursive))
                    $dir->addChild(new Dir($file));
                if(is_file($newPath))
                    $dir->addChild(new File($file));
            }
        }
        return $dir;
    }
	
}
?>
example.php
<?php
include("succes!.class.php");
function ToonMappenVanArray($array)
{
	$mappen = NULL;
	foreach($array as $i=>$bestand)
	{
		if(is_array($bestand))
		{
			$mappen .= '<li>'.$i.'<ul>';
			$mappen .= ToonMappenVanArray($bestand);
			$mappen .= '</ul></li>';
		}
		else
		{
			$mappen .='<li>'.$bestand.'</li>';
		}
	}
	return $mappen;
}

$builder = new FileNodeBuilder;
$data= $builder->build('./',true);
echo ToonMappenVanArray($data->buildArray());
?> 

Reacties

0
Nog geen reacties.