MasterACE14 Posted June 27, 2009 Share Posted June 27, 2009 I have this script... // get sub folders in directory function dir_list($d){ foreach(array_diff(scandir($d),array('.','..')) as $f)if(is_dir($d.'/'.$f))$l[]=$f; return $l; } // get files from folders function file_list($d,$x){ foreach(array_diff(scandir($d),array('.','..')) as $f)if(is_file($d.'/'.$f)&&(($x)?ereg($x.'$',$f):1))$l[]=$f; return $l; } $dir = dir_list("modules"); $file = file_list("modules",".php"); $files = array_merge($dir,$file); $count = count($files); for($i=0;$i<$count;$i++) { echo $files[$i]."<br />"; } my file/folder structure is... modules-- a.php b.php c.php f.php z.php helloworld-- helloworld.php test-- test.php I'm trying to get my script to grab all the files and their paths, and match them up. so my current output should be(not exact same order)... a.php b.php c.php f.php z.php helloworld/helloworld.php test/test.php but instead my current output is... helloworld test a.php b.php c.php f.php z.php so I want the file paths to each folder, whether it's in the root directory, or in sub folders. any help is GREATLY appreciated Regards, ACE Quote Link to comment https://forums.phpfreaks.com/topic/163880-solved-file-paths/ Share on other sites More sharing options...
Daniel0 Posted June 27, 2009 Share Posted June 27, 2009 /** * @param string $path The directory to get the tree of * @param string $directorySeparator Optional alternate directory separator than the OS' default * @param array $extensionLimit Limit return to specific extensions * @param string $startPath Start part to strip off (internal) */ function getDirectoryTree($path, $directorySeparator = DIRECTORY_SEPARATOR, array $extensionLimit = array(), $startPath = null) { if (!is_dir($path)) { throw new InvalidArgumentException('The given path is not a directory.'); } if (null === $startPath) { $startPath = $path; } $startPathLength = strlen($startPath) + 1; $items = array(); foreach (new DirectoryIterator($path) as $item) { if ($item->isDot()) continue; if ($item->isDir()) { $items = array_merge($items, getDirectoryTree($item->getPathname(), $directorySeparator, $extensionLimit, $startPath)); } else { $name = substr($item->getPathName(), $startPathLength); if (count($extensionLimit) && !in_array(pathinfo($name, PATHINFO_EXTENSION), $extensionLimit)) { continue; } if ($directorySeparator !== DIRECTORY_SEPARATOR) { $name = str_replace(DIRECTORY_SEPARATOR, $directorySeparator, $name); } $items[] = $name; } } return $items; } Then foreach (getDirectoryTree('C:/projects/phpfreaks/application', '/', array('php')) as $item) { echo $item . PHP_EOL; } outputs (on my laptop): Bootstrap.php models/Comments.php models/Content.php models/Feeds.php models/ForumUsers.php models/Snippets.php models/User.php models/Users.php models/Votes.php modules/default/controllers/CommentController.php modules/default/controllers/ContentController.php modules/default/controllers/ErrorController.php modules/default/controllers/FeedController.php modules/default/controllers/IndexController.php modules/default/controllers/SearchController.php modules/default/controllers/SnippetsController.php modules/default/controllers/UserController.php modules/default/controllers/VoteController.php modules/default/views/helpers/ContentBody.php modules/default/views/helpers/FormMultiText.php modules/default/views/helpers/Image.php modules/default/views/helpers/ParseData.php modules/default/views/helpers/Plural.php modules/default/views/helpers/Truncate.php If you don't actually care that it's the absolute path or don't care about the directory separators, you can remove that for extra performance. Quote Link to comment https://forums.phpfreaks.com/topic/163880-solved-file-paths/#findComment-864660 Share on other sites More sharing options...
MasterACE14 Posted June 27, 2009 Author Share Posted June 27, 2009 works perfectly! thanks! Quote Link to comment https://forums.phpfreaks.com/topic/163880-solved-file-paths/#findComment-864661 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.