Melvin Hagberg
Melvin Hagberg

Reputation: 220

PHP is_dir don't find folder

I want to loop through a directory and echo all folders. However, the foreach-loop just echo out . and ..

$dir = 'content/';
$handle = scandir($dir);
foreach ($handle as $file) {
    if (is_dir($file)) { echo '<br>' . $file; }
}

Here is the content of the directory:

enter image description here

Upvotes: 3

Views: 619

Answers (2)

RiggsFolly
RiggsFolly

Reputation: 94662

Correct, the only 2 Directories in that Directory are the . and .. folders. The FILES are not folders. So if you want to see the files, try

$dir = 'content/';
$handle = scandir($dir);
foreach ($handle as $file) {
    if ( ! is_dir($file)) { echo '<br>' . $file; }
}

Upvotes: 1

Chris
Chris

Reputation: 4728

Have a go using this:

$dir = 'content/';
$handle = scandir($dir);
foreach ($handle as $file) {
    if (is_dir($dir.'/'.$file)) { echo '<br>' . $file; }
}

Upvotes: 2

Related Questions