Orangeworker Posted October 4, 2011 Share Posted October 4, 2011 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/248433-list-all-files-in-a-directory-including-sub-folders/ Share on other sites More sharing options...
Orangeworker Posted October 4, 2011 Author Share Posted October 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/248433-list-all-files-in-a-directory-including-sub-folders/#findComment-1275765 Share on other sites More sharing options...
Orangeworker Posted October 5, 2011 Author Share Posted October 5, 2011 Anyone have any ideas? I'm pulling my hair out with this, been trying so much different code and can't get anything to work. Quote Link to comment https://forums.phpfreaks.com/topic/248433-list-all-files-in-a-directory-including-sub-folders/#findComment-1276068 Share on other sites More sharing options...
AbraCadaver Posted October 5, 2011 Share Posted October 5, 2011 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); } Quote Link to comment https://forums.phpfreaks.com/topic/248433-list-all-files-in-a-directory-including-sub-folders/#findComment-1276131 Share on other sites More sharing options...
markjoe Posted October 5, 2011 Share Posted October 5, 2011 Unless you want to be confined to a specific directory depth, recursion is definitively the right way to do this. (As posted by AbraCadaver) Quote Link to comment https://forums.phpfreaks.com/topic/248433-list-all-files-in-a-directory-including-sub-folders/#findComment-1276168 Share on other sites More sharing options...
Orangeworker Posted October 5, 2011 Author Share Posted October 5, 2011 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); } Quote Link to comment https://forums.phpfreaks.com/topic/248433-list-all-files-in-a-directory-including-sub-folders/#findComment-1276173 Share on other sites More sharing options...
AbraCadaver Posted October 5, 2011 Share Posted October 5, 2011 Uh, yeah... If you did not use php tags <?php and ?> then I think you need to back up and read a PHP book or tutorial or something before trying to write code. Quote Link to comment https://forums.phpfreaks.com/topic/248433-list-all-files-in-a-directory-including-sub-folders/#findComment-1276183 Share on other sites More sharing options...
Orangeworker Posted October 5, 2011 Author Share Posted October 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/248433-list-all-files-in-a-directory-including-sub-folders/#findComment-1276188 Share on other sites More sharing options...
djlee Posted October 5, 2011 Share Posted October 5, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248433-list-all-files-in-a-directory-including-sub-folders/#findComment-1276193 Share on other sites More sharing options...
AbraCadaver Posted October 5, 2011 Share Posted October 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/248433-list-all-files-in-a-directory-including-sub-folders/#findComment-1276195 Share on other sites More sharing options...
markjoe Posted October 5, 2011 Share Posted October 5, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248433-list-all-files-in-a-directory-including-sub-folders/#findComment-1276198 Share on other sites More sharing options...
Orangeworker Posted October 5, 2011 Author Share Posted October 5, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248433-list-all-files-in-a-directory-including-sub-folders/#findComment-1276225 Share on other sites More sharing options...
markjoe Posted October 5, 2011 Share Posted October 5, 2011 are you trying to open a url? opendir() expects a local path, not a web address. Quote Link to comment https://forums.phpfreaks.com/topic/248433-list-all-files-in-a-directory-including-sub-folders/#findComment-1276233 Share on other sites More sharing options...
Orangeworker Posted October 5, 2011 Author Share Posted October 5, 2011 I'm trying to open the .php file with the "example" address that I listed cause that's how I do it with other code that I've had a developer write for me. If that's incorrect, please tell me how I can execute this code. Quote Link to comment https://forums.phpfreaks.com/topic/248433-list-all-files-in-a-directory-including-sub-folders/#findComment-1276237 Share on other sites More sharing options...
AbraCadaver Posted October 5, 2011 Share Posted October 5, 2011 I'm totally confused about what you are doing now. Quote Link to comment https://forums.phpfreaks.com/topic/248433-list-all-files-in-a-directory-including-sub-folders/#findComment-1276289 Share on other sites More sharing options...
Orangeworker Posted October 6, 2011 Author Share Posted October 6, 2011 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>"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/248433-list-all-files-in-a-directory-including-sub-folders/#findComment-1276292 Share on other sites More sharing options...
thomasw_lrd Posted October 6, 2011 Share Posted October 6, 2011 It helps me, it's on my to do list, but very low on the priority. Now I can look like a genius for the boss. Quote Link to comment https://forums.phpfreaks.com/topic/248433-list-all-files-in-a-directory-including-sub-folders/#findComment-1276293 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.