Is_dir does not recognize folders

I am trying to make a function that scans a folder for subfolders, and then returns a numerical array with the names of these folders.

This is the code that I use for testing. As soon as I get it to print the names of folders, not just "." . and ".." for the current and previous folder everything will be fine, and I can finish the function.

<?php
function super_l_getthemes($dir="themes")
{

if ($handle = opendir($dir)) {
    echo "Handle: {$handle}\n";
    echo "Files:\n";


    while (false !== ($file = readdir($handle))) {
       echo "{$file}<br>";
    }

    closedir($handle);
}
?>

The code above works fine and prints out the entire contents of the folder: files, subfolders and "." and ".."

but if I replaced:

  while (false !== ($file = readdir($handle))) {
       echo "{$file}<br>";
    }

with:

while (false !== ($file = readdir($handle))) {
        if(file_exists($file) && is_dir($file)){echo "{$file}";}
    }

Function only prints "." and "..", not the two folder names that I would like to print.

Any help is appreciated.

+3
source share
8 answers

file_exists, .

while (false !== ($file = readdir($handle))) {
    $file_path = $dir . DIRECTORY_SEPARATOR . $file;
    if (file_exists($file_path) && is_dir($file_path)) {
        echo "{$file}";
    }
}
+5

readdir , .

, "foo" "/path/to/files/", readdir "/path/to/files/" , , "Foo".

, , script, , , (, , ), is_dir "foo".

, , readdir .


if ($handle = opendir($dir)) {
    echo "Handle: {$handle}\n";
    echo "Files:\n";

    while ($file = readdir($handle)) {
        /*** make $file into an absolute path ***/
        $absolute_path = $dir . '/' . $file;

        /*** NOW try stat'ing it ***/
        if (is_dir($absolute_path)) {
            /* it a directory; do stuff */
        }
    }

    closedir($handle);
}
+3

:

while (false !== ($file = readdir($handle))) {
    if(file_exists($dir.'/'.$file) && is_dir($dir.'/'.$file)){echo "{$file}";}
}

. http://php.net/readdir

+1

, :

glob('/some/path/to/search/in/*', GLOB_ONLYDIR);

. , , SPL RecursiveDirectoryIterator

$fileSystemIterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator('/some/path/to/look/in'),
    RecursiveIteratorIterator::SELF_FIRST);

foreach:

$directories = array();
foreach($fileSystemIterator as $path => $fileSystemObject) {
    if($fileSystemObject->isDir()) {
        $directories[] = $path;
    }
}

$directories .

+1

, file_exists, is_dir,

is_dir. :

is_dir TRUE, , FALSE .

:

while (false !== ($file = readdir($handle))) {
        if(is_dir($file)){echo "{$file}";}
    }

is_dir , .

0
$files = array();
foreach(new DirectoryIteraror('/path') as $file){
    if($file->isDir() /* && !$file->isDot()*/) $files[] = $file->getFilename();
}

[edit: although you wanted to skip the point, commented on this)

0
source
        $directory = scandir($path);
            foreach($directory as $a){
                if(is_dir($path.$a.'/') && $a != '.' && $a != '..'){
                echo $a.'<br/>';        
                }
            }

With the path shown, it displays the folders present in the path.

0
source

I agree with nuqqsa's solution, however I would like to add something to it.

Instead of specifying a path, you can change the current directory instead.

For instance,

// open directory handle
// ....
chdir($dir);
while (false !== ($file = readdir($handle)))
 if(is_dir($file))
  echo $file;
// close directory handle
-2
source

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


All Articles