PHP :: Aufgabe #2

6 Lösungen Lösungen öffentlich

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:

vote_ok
von bibir (1870 Punkte) - 09.09.2014 um 14:50 Uhr
Quellcode ausblenden PHP-Code
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();
vote_ok
von x9eleven (640 Punkte) - 27.03.2015 um 17:34 Uhr
Quellcode ausblenden PHP-Code
<?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("./"));
        ?>
vote_ok
von Code_Biene (610 Punkte) - 24.04.2015 um 16:22 Uhr
Quellcode ausblenden PHP-Code
	
	
	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('./');
	
	
	
vote_ok
von Jidoka86 (660 Punkte) - 08.05.2016 um 21:48 Uhr
Quellcode ausblenden PHP-Code
<?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("./");
1x
vote_ok
von Exception (7090 Punkte) - 02.06.2018 um 20:06 Uhr
Quellcode ausblenden PHP-Code
<?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 />';	
}

?>
vote_ok
von syneex (2670 Punkte) - 30.08.2018 um 10:27 Uhr
Quellcode ausblenden PHP-Code
<?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);
	}
}
?>
2095029

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.