0

I tried to make function to show all files and dir(if have) in one selected dir.

class Test{

private $directory;

    public function getDir($directory){
            $this->directory = realpath($directory);
            $scan = scandir($this->directory);

            foreach ($scan as $value) {

                if(!is_dir($this->directory.DIRECTORY_SEPARATOR.$value)){
                    echo '<span style="color:blue">'.$value.'</span><br>';
                }else{
                    echo '<span style="color:red">'.$value.'</span><br>';
                    //Here I tried to return getDir($value) - but I retype $directory any ideas ?
                }
            }
        }

I thought over this how to make but ... Little help will be really nice. Excuse my bad english.

Nick
  • 102
  • 9
  • Maybe if $directory is array, but I dont know ;) – Nick Feb 23 '16 at 18:36
  • http://stackoverflow.com/questions/15774669/list-all-files-in-one-directory-php or http://php.net/manual/en/function.scandir.php or http://www.sitepoint.com/list-files-and-directories-with-php/ or http://proger.i-forge.net/%D0%9A%D0%BE%D0%BC%D0%BF%D1%8C%D1%8E%D1%82%D0%B5%D1%80/PHP/[11.04.09]%20Recursively%20list%20all%20files%20in%20a%20directory.html – Alive to die - Anant Feb 23 '16 at 18:42
  • I know how to scan all files, but I want if one of the scaned files is dir to return function again. – Nick Feb 23 '16 at 18:46
  • one recursive way is also there in my given link – Alive to die - Anant Feb 23 '16 at 18:47

1 Answers1

1

Just use recursive way :

<?php
...
private $result;

public function getDir($directory) {
    $files = scandir(realpath($directory));

    foreach($files as $key => $value){
        $path = realpath($directory .DIRECTORY_SEPARATOR. $value);
        if(!is_dir($path)) {
            $this->results[] = '<span style="color:blue">'.$value.'</span><br>';
        } else if($value != "." && $value != "..") {
            $this->getDir($path);
            $this->results[] = '<span style="color:red">'.$value.'</span><br>';
        }
    }

    return $this->results;
}
Vincent Decaux
  • 9,857
  • 6
  • 56
  • 84