newbtophp Posted August 9, 2010 Share Posted August 9, 2010 Hi, I've tried various methods (ie. glob etc.) but haven't had any success, the purpose is to scan through all directories for php files, then place those php files along with their path ($dir/$filename) into an array (to use elsewhere) So if I have the following file/directory structure (example): --forums: ---topic.php ---forum.php --func: ---functions.php Would produce the following array: $files = array('topic.php' => 'forums/topic.php', 'forum.php' => 'forums/forum.php' 'functions.php' => 'func/functions.php'); Quote Link to comment https://forums.phpfreaks.com/topic/210251-array-of-filenames-from-directory/ Share on other sites More sharing options...
Alex Posted August 9, 2010 Share Posted August 9, 2010 You can't use that array structure. It's possible that two files have the same name but different paths, and you can't have two elements with the same index (obviously). Quote Link to comment https://forums.phpfreaks.com/topic/210251-array-of-filenames-from-directory/#findComment-1097199 Share on other sites More sharing options...
schilly Posted August 9, 2010 Share Posted August 9, 2010 Assuming no files are named the same. Try something like this: function phpFiles($baseDir,&$files){ $handle = opendir($baseDir); while (false !== ($file = readdir($handle))){ $file_path = $baseDir . "/$file"; //check for directory if(is_dir($file_path)){ phpFiles($file_path, $files); } else { //check for php file $extension = strtolower(substr(strrchr($filename, '.'), 1)); if($extension == 'php'){ // add to array $files[$file] = $file_path; } } } closedir($handle); } $file_array = array(); phpFiles('baseDir',$file_array); I haven't tested it but it should be a good start. Quote Link to comment https://forums.phpfreaks.com/topic/210251-array-of-filenames-from-directory/#findComment-1097202 Share on other sites More sharing options...
newbtophp Posted August 9, 2010 Author Share Posted August 9, 2010 You can't use that array structure. It's possible that two files have the same name but different paths, and you can't have two elements with the same index (obviously). Their won't be duplicates/same names, as I will be creating the file/filenames to ensure their aint. Quote Link to comment https://forums.phpfreaks.com/topic/210251-array-of-filenames-from-directory/#findComment-1097204 Share on other sites More sharing options...
Alex Posted August 9, 2010 Share Posted August 9, 2010 Why not just store the entire path and not the filename? You can easily get the filename later using pathinfo. Here's a more simplistic approach using the RecursiveDirectoryIterator: $files = array(); $it = new RecursiveDirectoryIterator('path/to/dir'); foreach(new RecursiveIteratorIterator($it) as $path) { if(pathinfo($path, PATHINFO_EXTENSION) == 'php') { $files[] = $path; } } If you're absolutely sure that their won't be duplicates you can change this: $files[] = $path; to: $files[pathinfo($path, PATHINFO_BASENAME)] = $path; But I would not recommend it. If you come back to this code 6 months later chances are you aren't going to remember that you cannot duplicate files, and when you do put in a duplicate file you'll be beating yourself up when you can't find the problem. It's always better to code safe and leave less potential for problems later. If anything it would be a better idea to have the index as the path and the filename as the value because you know that filenames can not be duplicated. That makes much more sense logically as well. Quote Link to comment https://forums.phpfreaks.com/topic/210251-array-of-filenames-from-directory/#findComment-1097208 Share on other sites More sharing options...
newbtophp Posted August 9, 2010 Author Share Posted August 9, 2010 Ok thanks Alex! /Solved Quote Link to comment https://forums.phpfreaks.com/topic/210251-array-of-filenames-from-directory/#findComment-1097214 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.