Displays all files and all directories

<?php
$dir = "*.*";
foreach(glob($dir) as $file)   {echo $file . "<br />";  }
?>

The code above displays all files in the directory.
You can see the result of the code above at http://dot.kr/x-test/displayDir/test.php
.

But actually the directory “displayDir” has not only some files but aslo some directories(folders).
I like to display not only all files but also all directories in the directory.

$dir = "*.*";

looks for names with a dot in it. Change it to:

<?php 
$dir = "*";
foreach(glob($dir) as $file)   {echo $file . "<br />";  }   
?>

Your code works fine.
Thank you, captainccs.

By the way, How can I sort them directories first and files later?

Get the in two separate queries. Unfortunately I don’t know how to build the regex for directories only:

// get directories
$dir = "/regex goes here/"; 
foreach(glob($dir) as $directory)   {echo $directory . "<br />";  }    

// get files
$dir = "*.*"; 
foreach(glob($dir) as $file)   {echo $file . "<br />";  }

Since I can’t get my head around regex, I use the old fashioned no-regex:

$dirs = scandir(getcwd());
echo "Directories:<br />\
";
$files = array();
foreach($dirs AS $name) {
    if(strpos($name, '.') === FALSE) {  // only directories
        echo $name . "<br />\
";
    } elseif(strpos($name, '.') !== 0) {  // drop system files
        $files[] = $name; 
    }
}
echo "<br />Files:<br />\
";
foreach($files as $file) {
    echo $file . "<br />\
";
}   

Might try…


<?php
$directory = "*";
$files = glob($directory . "*");
foreach($files as $file) { if(is_dir($file)) { $dir_array[] = $file; }else{ $file_array[] = $file; } }
if(count($dir_array)>0) { $i=0; while($i<count($dir_array)) { echo "folder = " . $dir_array[$i] . "<br>"; $i ++; }}
echo "<hr>";
$i=0;
while($i<count($file_array)){ echo "file = " . $file_array[$i] . "<br>"; $i ++; }
?>