herghost Posted January 19, 2012 Share Posted January 19, 2012 Hi all I am using this function to list all files and folders within a directory function getFilesFromDir($dir) { $files = array(); if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if(is_dir($dir.'/'.$file)) { $dir2 = $dir.'/'.$file; $files[] = getFilesFromDir($dir2); } else { $files[] = $dir.'/'.$file; } } } closedir($handle); } return array_flat($files); } function array_flat($array) { foreach($array as $a) { if(is_array($a)) { $tmp = array_merge($tmp, array_flat($a)); } else { $tmp[] = $a; } } return $tmp; } When I call the function I give the dir as a variable, however if the dir I want is a sub-dir of a current dir then the list starts from the main dir Basically say my file test.html is contained here files/test/test.html, the input I need to give is files/test/. This would then output in the array ->files/test/test.html. What I would like the output to be is test/test.html. How do I edit my recursive function to start from the final folder in the input? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/255378-help-with-scandir-function/ Share on other sites More sharing options...
dzelenika Posted January 19, 2012 Share Posted January 19, 2012 function getFilesFromDir($dir) { $files = array(); if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if(is_dir($dir.'/'.$file)) { $dir2 = $dir.'/'.$file; // $files[] = getFilesFromDir($dir2); $files = getFilesFromDir($dir2) + $files; // array union operator } else { $files[] = $dir.'/'.$file; } } } closedir($handle); Quote Link to comment https://forums.phpfreaks.com/topic/255378-help-with-scandir-function/#findComment-1309362 Share on other sites More sharing options...
herghost Posted January 19, 2012 Author Share Posted January 19, 2012 Thanks, however I am getting an Unsupported operand types error on the union array line? I am guessing files() is empty and its trying to divide by 0? Quote Link to comment https://forums.phpfreaks.com/topic/255378-help-with-scandir-function/#findComment-1309381 Share on other sites More sharing options...
darkfreaks Posted January 21, 2012 Share Posted January 21, 2012 try this: function ListFiles($dir) { if($dh = opendir($dir)) { $files = Array(); $inner_files = Array(); while($file = readdir($dh)) { if($file != "." && $file != ".." && $file[0] != '.') { if(is_dir($dir . "/" . $file)) { $inner_files = ListFiles($dir . "/" . $file); if(is_array($inner_files)) $files = array_merge($files, $inner_files); } else { array_push($files, $dir . "/" . $file); } } } closedir($dh); return $files; } } example: foreach(Listfile('path to directory') as $key=>file) { echo $file."<br />"} Quote Link to comment https://forums.phpfreaks.com/topic/255378-help-with-scandir-function/#findComment-1309761 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.