guyfromfl Posted January 19, 2011 Share Posted January 19, 2011 I have a script that works perfectly, for one type of file extension. What I want to do now is be able to list all files in a set of file types. I want to pass the argument to the fileSystem class's loadFiles method like this: $some_var = $fileSystem->loadFiles(DATA_DIRECTORY, "csv,xls,etc"); Then in the loadFiles method it reads the dir for each file, then test its extension. If they are of the right type, then push it on the returned array. Now I am having problems testing if the file's extension is in the array. If I use if (strpos(getExt($file), $array_of_wanted_exts))... php would match php3.. Does anybody have a better method? Link to comment https://forums.phpfreaks.com/topic/225011-best-way-to-list-files-in-a-directory-by-extension/ Share on other sites More sharing options...
guyfromfl Posted January 19, 2011 Author Share Posted January 19, 2011 What if I exploded $$array_of_wanted_exts and searched the extensions in that list? Link to comment https://forums.phpfreaks.com/topic/225011-best-way-to-list-files-in-a-directory-by-extension/#findComment-1162165 Share on other sites More sharing options...
ignace Posted January 19, 2011 Share Posted January 19, 2011 class RecursiveFileExtensionFilterIterator extends RecursiveFilterIterator { private $validFileExtensions = array(); public function __construct($recursiveIterator, $validFileExtensions) { $this->validFileExtensions = $validFileExtensions; parent::__construct($recursiveIterator); } public function accept() { return in_array(pathinfo($this->current(), PATHINFO_EXTENSION), $this->validFileExtensions); } } foreach(new RecursiveFileExtensionFilterIterator(new RecursiveDirectoryIterator('.'), array('php')) as $file) { echo $file->getFilename(), "\r\n"; } Link to comment https://forums.phpfreaks.com/topic/225011-best-way-to-list-files-in-a-directory-by-extension/#findComment-1162177 Share on other sites More sharing options...
guyfromfl Posted January 19, 2011 Author Share Posted January 19, 2011 damn, thats sexy. I'm out of here in 10 minutes, so I'll build a class tomorrow and let you know how it goes. Thanks! Link to comment https://forums.phpfreaks.com/topic/225011-best-way-to-list-files-in-a-directory-by-extension/#findComment-1162206 Share on other sites More sharing options...
AbraCadaver Posted January 19, 2011 Share Posted January 19, 2011 That is sexy, but I would go for simpler. Either pass in an array of file extensions, or explode for one and use in_array() if you're looping through a directory with readdir() or some such. But I would probably glob(): function loadFiles($directory, $extensions) { $pattern = '{*.' . implode(',*.', explode(',', $extensions)) . '}'; return glob($directory . '/' . $pattern, GLOB_BRACE); } $some_var = loadFiles(DATA_DIRECTORY, "csv,xls,etc"); Link to comment https://forums.phpfreaks.com/topic/225011-best-way-to-list-files-in-a-directory-by-extension/#findComment-1162229 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.