Problem calling php method from inside

class styleFinder{

function styleFinder(){

}

function getFilesNFolders($folder){

    $this->folder = $folder ;
    if($this->folder==""){
        $this->folder = '.';
    }
    if ($handle = opendir($this->folder)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                echo "$file<br /> ";
                if(is_dir($file)){

                    echo "<b>" . $file . " is a folder</b><br />&nbsp;&nbsp;&nbsp;with contents ";
                    $this::getFilesNFolders($file);
                   # echo "Found folder";
                }
            }
        }
        closedir($handle);
    }
}

} I want to print a complete tree of folders and files, the script goes into the first folders and finds the files, and then finds any subfolders, but not subfolders (and yes there are some). Any ideas please?

+3
source share
5 answers
$this::getFilesNFolders($file);

It should be

$this->getFilesNFolders($file);
+7
source

With PHP 5.1.2, you have this useful class: http://www.php.net/manual/en/class.recursivedirectoryiterator.php

+6
source

:

$this->functionName():
+2

, RecursiveDirectoryIterator:

$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator('/path/to/directory'),
        RecursiveIteratorIterator::SELF_FIRST);

foreach($iterator as $fileObject) {
    if($fileObject->isDir()) {
        echo "<strong>$fileObject is a folder:</strong><br>\n";
    } else {
        echo $fileObject, "<br>\n";
    }
}
+1

As others have said, in the method itself you need to call getFilesNFolders with $this -> getFilesNFolders($file). Also, the way the code was sent at the end is missing at the end, but since it starts after the code, it is probably a typo. The following code worked for me (I ran through the command line, so I added code for indenting different levels of directories, as well as \ n's output):

<?php

class StyleFinder{
function StyleFinder(){
}

function getFilesNFolders($folder, $spaces){

    $this->folder = $folder ;
    if($this->folder==""){
        $this->folder = '.';
    }
    if ($handle = opendir($this->folder)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                if(is_dir($file)){
                    echo $spaces . "<b>" . $file . " is a folder</b><br/>&nbsp;&nbsp;&nbsp;with contents:\n";
                    $this -> getFilesNFolders($file, $spaces . "  ");
                } else {
                    echo $spaces . "$file<br />\n";
                }
            }
        }
        closedir($handle);
    }
}
}

$sf = new StyleFinder();
$sf -> getFilesNFolders(".", "");

?>
0
source

Source: https://habr.com/ru/post/1761394/


All Articles