PHP get file listing including sub directories
from glob example
if ( ! function_exists('glob_recursive'))
{
// Does not support flag GLOB_BRACE
function glob_recursive($pattern, $flags = 0)
{
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
{
$files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
}
return $files;
}
}
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 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.
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.
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 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 out files in PHP (ONLY the files) as links from all subdirectories of a directory
Try using glob():
<?php
foreach(glob('path-to/UPLOAD_FOLDER/*') as $subDirectory) {
foreach(glob($subDirectory'./*') as $file) {
// do what you want with file here... example:
$file_list .= $file.'<br>';
}
}
echo $file_list;
?>
Related Topics
Jquery Ajax Form Using Mail() PHP Script Sends Email, But Post Data from HTML Form Is Undefined
Pre-Declare All Private/Local Variables
What Is Pdo & Why Should I Use It
Best Practices: Working with Long, Multiline Strings in PHP
Passing an Array Using an HTML Form Hidden Element
Extract Text from Doc and Docx
Check If Specific Array Key Exists in Multidimensional Array - PHP
Preventing Caching of CSS Files
Post and Get at the Same Time in PHP
Object of Class Datetime Could Not Be Converted to String
Custom User Authentication Base on the Response of an API Call
Checking If Array Is Multidimensional or Not
Regular Expression to Collect Everything After the Last /
PHP - Get Base64 Img String Decode and Save as Jpg (Resulting Empty Image )