Nick
Nick

Reputation: 102

function which show all files and directories

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.

Upvotes: 0

Views: 121

Answers (1)

Vincent Decaux
Vincent Decaux

Reputation: 10714

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;
}

Upvotes: 1

Related Questions