• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • About
  • Projects
    • GStaticMap WP Plugin
  • Contact
  • Privacy Policy

Lorenz Blog

All About Web & Mobile Application Development

  • Featured Articles
  • Gadgets
    • Android
    • Blackberry
  • Programming
    • Android
    • PHP
    • Java Script
    • MySQL
    • Postgresql
    • Flex
    • Web
  • Software
    • Mac OS
    • Windows
    • Linux
  • Web
You are Here » Home >> Information Technology >> Programming >> PHP >> How to List A Directory Contents Using PHP (Recursive)

How to List A Directory Contents Using PHP (Recursive)

December 4, 2009 by Lorensius Londa 1 Comment

To list contents of a directory is simple in PHP, just use three PHP directory functions, opendir,  readdir and is_dir.

The code:

<?php
function listDirectory($path)
{
    $handle = @opendir($path);

    while (false !== ($file = readdir($handle))) {
        if ($file == '.' || $file == '..') continue;

        if ( is_dir("$path/$file")) {
            echo "$path/$file\n";
            listDirectory("$path/$file");
        } else {
            echo "$path/$file\n";
        }
    }

    closedir($handle);
}

listDirectory("tutorials");
?>

Here’s the result:

Directory List Using PHP
Directory List Using PHP

To start list contents, first open the directory handle using opendir then loop to read the entire contents using readdir. Check if content is a directory or a file using is_dir, if a directory,  then call function listDirectory to read it’s content (recursive).TV6CA39U8TTG

Facebooktwitterredditpinterestlinkedinmailby feather

Related posts:

  1. How to Set Time Zone in PHP and MySQL
  2. How To Define Global Absolute Path in PHP
  3. How to Zip Files or Folder On The Fly Using PHP
  4. How to Create Facebook Application with PHP for Beginner

Filed Under: PHP, Programming Tagged With: directory, directory list, PHP, php directory content, php directory list, php directory tree, recursive, tree

About Lorensius Londa

Passionate web and mobile application developer. Co-founder of TRUSTUDIO, loves programming, Android, aviation, travelling, photography, coffee and gym mania.

Reader Interactions

Comments

  1. php enthusiast says

    July 7, 2012 at 4:57 pm

    Just what I was looking for :).

    Thank you and take care!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About Me

A husband, father of two, passionate software developer, diy lover and home baker who loves to learn new things. Read More…

  • Facebook
  • GitHub
  • Google+
  • Instagram
  • Twitter
  • YouTube

Featured Articles

How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Blue Bamboo P25 Printer Android Demo Application With Source Code

Simple JSON RPC Client for Android

How to Send Message to Google Cloud Messaging (GCM) Server Using JSON and PHP

Footer

Recent Comments

  • Aditya Dabas on About
  • Ayten Göksenin Barutçu on How to Make Android Map Scrollable Inside a ScrollView Layout
  • mang jojot on About
  • Hussain on How to Programmatically Scan or Discover Android Bluetooth Devices

Recent Posts

  • How to Fix Blank Screen on WordPress Add/Edit Post Page
  • How to Programmatically Restart the ESP32 Board
  • How to Get Hardware Info of ESP32
  • How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Latest Tweets

To protect our users from spam and other malicious activity, this account is temporarily locked. Please log in to https://twitter.com to unlock your account.

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in