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);

?>

I understand that this is a common task, but I can't find any code that works for me. I've done a lot of searching on php.net. I either get a blank screen or errors. The code above works for me which is why I want to see if it can be altered.

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);
}

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); }

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.

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

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.

scandir function returns a file name as a string.

you need to check if path is a directory, not just the filename.

if(is_dir($path.'/'.$file)

 

http://us.php.net/manual/en/function.readdir.php

http://us.php.net/manual/en/function.is-dir.php

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

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>";
                }
        }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.