PHP Get all subdirectories of a given directory
Option 1:
You can use glob()
with the GLOB_ONLYDIR
option.
Option 2:
Another option is to use array_filter
to filter the list of directories. However, note that the code below will skip valid directories with periods in their name like .config
.
$dirs = array_filter(glob('*'), 'is_dir');
print_r($dirs);
List all folders in directory (PHP)
$d = dir(".");
echo "<ul>";
while (false !== ($entry = $d->read()))
{
if (is_dir($entry) && ($entry != '.') && ($entry != '..'))
echo "<li><a href='{$entry}'>{$entry}</a></li>";
}
echo "</ul>";
$d->close();
How to list directories, sub-directories and all files in php
Use scandir to see all stuff in the directory and is_file
to check if the item is file or next directory, if it is directory, repeat the same thing over and over.
So, this is completely new code.
function listIt($path) {
$items = scandir($path);
foreach($items as $item) {
// Ignore the . and .. folders
if($item != "." AND $item != "..") {
if (is_file($path . $item)) {
// this is the file
echo "-> " . $item . "<br>";
} else {
// this is the directory
// do the list it again!
echo "---> " . $item;
echo "<div style='padding-left: 10px'>";
listIt($path . $item . "/");
echo "</div>";
}
}
}
}
echo "<div style='padding-left: 10px'>";
listIt("/");
echo "</div>";
You can see the live demo here in my webserver, btw, I will keep this link just for a second
When you see the "->" it's an file and "-->" is a directory
The pure code with no HTML:
function listIt($path) {
$items = scandir($path);
foreach($items as $item) {
// Ignore the . and .. folders
if($item != "." AND $item != "..") {
if (is_file($path . $item)) {
// this is the file
// Code for file
} else {
// this is the directory
// do the list it again!
// Code for directory
listIt($path . $item . "/");
}
}
}
}
listIt("/");
the demo can take a while to load, it's a lot of items.
List all the files and folders in a Directory with PHP recursive function
Get all the files and folders in a directory, don't call function when you have .
or ..
.
Your code :
<?php
function getDirContents($dir, &$results = array()) {
$files = scandir($dir);
foreach ($files as $key => $value) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (!is_dir($path)) {
$results[] = $path;
} else if ($value != "." && $value != "..") {
getDirContents($path, $results);
$results[] = $path;
}
}
return $results;
}
var_dump(getDirContents('/xampp/htdocs/WORK'));
Output (example) :
array (size=12)
0 => string '/xampp/htdocs/WORK/iframe.html' (length=30)
1 => string '/xampp/htdocs/WORK/index.html' (length=29)
2 => string '/xampp/htdocs/WORK/js' (length=21)
3 => string '/xampp/htdocs/WORK/js/btwn.js' (length=29)
4 => string '/xampp/htdocs/WORK/js/qunit' (length=27)
5 => string '/xampp/htdocs/WORK/js/qunit/qunit.css' (length=37)
6 => string '/xampp/htdocs/WORK/js/qunit/qunit.js' (length=36)
7 => string '/xampp/htdocs/WORK/js/unit-test.js' (length=34)
8 => string '/xampp/htdocs/WORK/xxxxx.js' (length=30)
9 => string '/xampp/htdocs/WORK/plane.png' (length=28)
10 => string '/xampp/htdocs/WORK/qunit.html' (length=29)
11 => string '/xampp/htdocs/WORK/styles.less' (length=30)
Listing all the folders subfolders and files in a directory using php
function listFolderFiles($dir){
$ffs = scandir($dir);
unset($ffs[array_search('.', $ffs, true)]);
unset($ffs[array_search('..', $ffs, true)]);
// prevent empty ordered elements
if (count($ffs) < 1)
return;
echo '<ol>';
foreach($ffs as $ff){
echo '<li>'.$ff;
if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
echo '</li>';
}
echo '</ol>';
}
listFolderFiles('Main Dir');
How to list all files in folders and sub-folders using scandir and display them as an option in select?
While the other answers are fine and correct, allow me to add my solution as well:
function dirToOptions($path = __DIR__, $level = 0) {
$items = scandir($path);
foreach($items as $item) {
// ignore items strating with a dot (= hidden or nav)
if (strpos($item, '.') === 0) {
continue;
}
$fullPath = $path . DIRECTORY_SEPARATOR . $item;
// add some whitespace to better mimic the file structure
$item = str_repeat(' ', $level * 3) . $item;
// file
if (is_file($fullPath)) {
echo "<option>$item</option>";
}
// dir
else if (is_dir($fullPath)) {
// immediatly close the optgroup to prevent (invalid) nested optgroups
echo "<optgroup label='$item'></optgroup>";
// recursive call to self to add the subitems
dirToOptions($fullPath, $level + 1);
}
}
}
echo '<select>';
dirToOptions();
echo '</select>';
It uses a recursive call to fetch the subitems. Note that I added some whitespace before each item to better mimic the file structure. Also I closed the optgroup
immediatly, to prevent ending up with nested optgroup
elements, which is invalid HTML.
How to list all files of a directory and its subdirectories in php?
I have found a solution.
I have created a script, by using this we can get or list all the files of a directory and its sub-directories.
Its working fine.In there mydir assumed as the directory path
function listDirFiles($dir){
foreach(glob("$dir/*") as $ff){
is_dir($ff) ? listDirFiles($ff) : $GLOBALS['a'][] = basename($ff);
}
}
listDirFiles('mydir');
echo "<pre>", print_r($GLOBALS['a']), "</pre>";
Related Topics
Why Are My Cookies Not Setting
PHP Json_Decode() Returns Null With Valid Json
Run a PHP File in a Cron Job Using Cpanel
MySQLi Count(*) Always Returns 1
How to Remove Non-Alphanumeric Characters
Amazon S3 - How to Fix 'The Request Signature We Calculated Does Not Match the Signature' Error
Configure Wamp Server to Send Email
How to Get Single Value from This Multi-dimensional PHP Array
Find the Last Element of an Array While Using a Foreach Loop in PHP
PHP Variable in HTML No Other Way Than: ≪PHP Echo $Var; ≫
Finding the Subsets of an Array in PHP
Does File_Get_Contents() Have a Timeout Setting
Why Is PHP Session_Destroy() Not Working
MySQL Datetime Fields and Daylight Savings Time - How to Reference the "Extra" Hour
Redefine Class Methods or Class