Jump to content

List all Files in a Directory Including Sub-Folders


Orangeworker

Recommended Posts

I came accross this code on YouTube and it works great for listing the content of a folder that the .php file is in.

However, how can I alter this code to list files of all folders and sub-folders? I thought plugging in an asterik might

work on the "$path =" line, but it didn't.

 

<?php
$path = ".";

$handle = opendir($path);
while ($file = readdir($handle)) {
echo $file . "<br>";
}
closedir($handle);

?>

Link to comment
Share on other sites

Not my preferred solution, but to use what you have posted:

 

$path = ".";
list_all_files($path);

function list_all_files($path) {
  $handle = opendir($path);
  while ($file = readdir($handle)) {
    if($file != '.' && $file != '..') {
      echo $file . "<br>";

      if(is_dir($file)) {
list_all_files("$path/$file");
      }
    }
  }
  closedir($handle);
}

Link to comment
Share on other sites

Thanks for your response Shawn. When I run the code, this is what I get in my browser. Looks to me like the same code which you altered.

If you have any other ideas, I'm totally open to it. Whatever you think will work.

 

 

$path = "."; list_all_files($path); function list_all_files($path) { $handle = opendir($path); while ($file = readdir($handle)) { if($file != '.' && $file != '..') { echo $file . "
"; if(is_dir($file)) { list_all_files("$path/$file"); } } } closedir($handle); }

Link to comment
Share on other sites

thats because it runs from the current working directory. simply give it a path to start from.

 

$path = "."; //-- current directory of the php executing script

$path = ".."; //--- the first parent folder of he current working directory

$path = "/"; //--- the root of the system (not a good idea)

$path = "~"; //--- the home directory of the executing user (depends on server setup)

$path = "/absolute/path/to/folder/to/start/at"; //-- probably the one you want (i.e. /var/www/)

 

Those are based on linux but should give you an idea of how  paths work in regards to the execution environment

Link to comment
Share on other sites

Ha, can't believe I missed that. I've been working with so much code lately (mostly VBA) that I'm burnt out. Sorry!

 

The revised code is working, but it's not showing sub-folders. It's only showing the files/folders that the .php file is in.

 

It's showing all files, sub dirs and files and sub dirs and sub dirs of the current dir.  If you want a different directory then change the path.

Link to comment
Share on other sites

This is what I get when I list the absolute path on line

 

$path = "example.com/test/list.php";

 

and I've got the list.php file (which has the code in it) in the "test" folder.

 

 

Warning: opendir(example.com/test/list.php) [function.opendir]: failed to open dir: No such file or directory in /home/content/12/1234567/html/test/list.php on line 6

 

Warning: readdir(): supplied argument is not a valid Directory resource in /home/content/12/1234567/html/test/list.php on line 7

 

Warning: closedir(): supplied argument is not a valid Directory resource in /home/content/12/1234567/html/test/list.php on line 19

Link to comment
Share on other sites

The code below does exactly what I want -- to simply display all files in folders/subfolders right in my browser.

All I did was place the .php file into the folder (on my server) that I was looking to view. Then I opened the file

with this path in my browser.

 

For example, the .php file on my server was in /test/list.php

The address I used in my browser to open the file was example.com/test/list.php

 

This was painful...I hope it helps someone. Thanks to everyone for your help.

 

<?php
filesInDir('.');
function filesInDir($tdir)
{
        $dirs = scandir($tdir);
        foreach($dirs as $file)
        {
                if (($file == '.')||($file == '..'))
                {
                }
                elseif (is_dir($tdir.'/'.$file))
                {
                        filesInDir($tdir.'/'.$file);
                }
                else
                {
                        echo $tdir.'/'.$file."<br>";
                }
        }
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.