PHP :: Aufgabe #2
6 Lösungen
Ordner rekursiv auslesen
Anfänger - PHP
von ne0n
- 10.08.2012 um 17:38 Uhr
Erstelle in einer Klasse eine Funktion die einen Ordner rekursiv durchläuft und die Unterverzeichnisse und Dateien ausgibt.
Lösungen:
class c_ordner_ausgeben{
private $folder;
private $tabs;
public function __construct($start, $tabs = 0){
$this->folder = $start;
$this->tabs = $tabs;
}
public function print_folder(){
$handle = opendir($this->folder);
while($eintrag = readdir($handle)){
if(($eintrag != '.') && ($eintrag != '..')){
if(is_dir($this->folder.'/'.$eintrag)){
echo '<div style="width:'.(15*$this->tabs).'px;float:left;"> </div><b>'.$eintrag.'</b>: dir<br clear="both">';
$n_folder = new c_ordner_ausgeben($this->folder.'/'.$eintrag, ($this->tabs+1));
$n_folder->print_folder();
} else {
echo '<div style="width:'.(15*$this->tabs).'px;float:left;"> </div>'.$eintrag.': file<br clear="both">';
}
}
}
}
}
$folder = new c_ordner_ausgeben('./');
$folder->print_folder();
<?php
function rekursivDir($sPath) {
$files = array();
foreach (new DirectoryIterator($sPath) as $ordnerInhalt) {
if ($ordnerInhalt->isDir()) {
(!$ordnerInhalt->isDot() ? $files[$ordnerInhalt->getFilename()] = rekursivDir($ordnerInhalt->getPathname()) : 0);
continue;
}
$files[] = $ordnerInhalt->getFilename();
} return $files;
}
print_r(rekursivDir("./"));
?>
class RekursivClass{
public function verzeichnis($dir)
{
$this->verzeichnis_lesen($dir);
}
public function verzeichnis_lesen($dir)
{
$handle_dir = realpath($dir)."/";
$scan_dir = scandir($handle_dir);
echo "<ul>";
foreach ($scan_dir as $file){
if($file =="." || $file =="..")
{
continue;
}
echo "<li>";
echo $file;
if(is_dir($handle_dir.$file) && is_readable($handle_dir.$file))
{
$this->verzeichnis_lesen($handle_dir.$file);
}
echo "</li>";
}
echo "</ul>";
}
}
$RD = new RekursivClass();
$RD -> verzeichnis('./');
<?php
/**
*
*/
class RecursivFolderPrinter
{
public function doPrint($path){
if(!file_exists($path)){
throw new Exception("File/Folder {$path} does not exist.");
}
//the only output call is here
echo realpath($path) . "\n";
//here goes the recursive logic
if(is_dir($path)){
$files = scandir($path);
foreach ($files as $file) {
if($file != "." and $file != ".."){
//here the root path must be prepended to prevent infinite loops
//in case of a child folder has the same name as it's parent.
//furthermore it's necessary to jump in to deeper levels.
$this->doPrint($path."/".$file);
}
}
}
}
}
$folderPrinter = new RecursivFolderPrinter();
$folderPrinter->doPrint("./");
<?php
class Files
{
public static function listFiles($dirPath)
{
if(!is_dir($dirPath))
{
throw new Exception("Eingegebener Pfad ist kein Verzeichnis.");
}
$items = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirPath), RecursiveIteratorIterator::SELF_FIRST);
foreach ($items as $item)
{
echo $item.'<br />';
}
}
}
try
{
Files::listFiles("C:\\xampp\\htdocs");
}
catch (Exception $ex)
{
echo $ex->getMessage().'<br />';
}
?>
<?php
$pfad = "C:\\xampp\\htdocs";
RekursiveDirectory::displayDirectory($pfad);
class RekursiveDirectory
{
function displayDirectory($path)
{
$filehandle = opendir($path);
while($singlefile = readdir($filehandle))
{
if($singlefile != "." && $singlefile != "..")
{
if(is_dir($path . "\\" . $singlefile))
{
RekursiveDirectory::displayDirectory($path . "\\" . $singlefile);
}
else
{
echo $path . "|-" . $singlefile . "<br>";
}
}
}
closedir($filehandle);
}
}
?>