Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

List All Files In Folder

Posted on September 23, 2015 By David Kittell
<?PHP
function getDirectory( $path = '.', $level = 0 ){ 
    $ignore = array( 'cgi-bin', '.', '..' ); 
    // Directories to ignore when listing output. Many hosts 
    // will deny PHP access to the cgi-bin. 

    $dh = @opendir( $path ); 
    // Open the directory to the handle $dh 
     
    while( false !== ( $file = readdir( $dh ) ) ){ 
    // Loop through the directory 
     
        if( !in_array( $file, $ignore ) ){ 
        // Check that this file is not to be ignored 
             
            $spaces = str_repeat( '&nbsp;', ( $level * 4 ) ); 
            // Just to add spacing to the list, to better 
            // show the directory tree. 
             
            if( is_dir( "$path/$file" ) ){ 
            // Its a directory, so we need to keep reading down... 
             
                echo "<strong>$spaces $file</strong><br>"; 
                getDirectory( "$path/$file", ($level+1) ); 
                // Re-call this same function but on a new directory. 
                // this is what makes function recursive. 
             
            } else { 
			
				$filesize = filesize($path . "/" . $file) . ' bytes';
								
                echo "$spaces $file | $filesize | $path<br>"; 
                // Just print out the filename 
             
            }          
        }      
    }      
    closedir( $dh ); 
    // Close the directory handle 
} 
getDirectory('/home/someuser'); 
?>

Reference: http://www.codingforums.com/post-a-php-snippet/71882-recursive-directory-listing-show-full-directory-structure.html

All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code PHP

Post navigation

Previous post
Next post

Related Posts

Windows 10 – Chocolatey Developer Setup

Posted on June 26, 2018October 7, 2020

# Install Chocolatey if not installed # Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString(‘https://chocolatey.org/install.ps1’)) Write-Host "Installing VSCode…" choco install vscode -y Write-Host "Installing Slack…" choco install slack -y Write-Host "Installing Git…" choco install git -y Write-Host "Installing 7zip…" choco install 7zip -y # OPTIONAL…

Read More

ICD-9 Generator

Posted on August 21, 2014October 26, 2015

<%@ Page Language="C#"%> <%@ Import Namespace="System" %> <%@ Import Namespace="System.Collections.Generic" %> <%@ Import Namespace="System.Collections.Specialized" %> <%@ Import Namespace="System.Configuration" %> <%@ Import Namespace="System.Data.Common" %> <%@ Import Namespace="System.Data.SqlClient" %> <%@ Import Namespace="System.Drawing" %> <%@ Import Namespace="System.Drawing.Printing" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Net.Mail" %> <%@ Import Namespace="System.Text" %> <%@ Import Namespace="System.Text.RegularExpressions"…

Read More

Swift – System Uptime

Posted on October 18, 2017October 18, 2017

I specifically use this in an iOS app but you should be able to use this in iOS or Mac OS The first function will get the Uptime of the system in seconds while the second function will convert the seconds into a easier to read/understand format. func uptime() ->…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes