2

How can I list all directories in Yii2?

I know there is a FileHelper to list all files in a directory/subdirectory, but I could not list directory using that.

Of course I can use raw PHP to do this, but before that I want to check if Yii2 provides any sort of functionality for it.

rob006
  • 21,383
  • 5
  • 53
  • 74
leninhasda
  • 1,620
  • 11
  • 18

3 Answers3

3

Since Yii 2.0.14 you can use findDirectories() method:

$allFiles = FileHelper::findDirectories('/path/to', ['recursive' => false]);
rob006
  • 21,383
  • 5
  • 53
  • 74
Gor Bert
  • 33
  • 3
1

You should have to use FileHelper.

$allFiles = \yii\helpers\FileHelper::findFiles('/path/to');

after that, you can access all file with $allFiles variable.

Noman
  • 1,459
  • 2
  • 18
  • 38
1

As far as I remember, Yii 2 does not provide such functionality, I also checked the latest version of BaseFileHelper in the sources and didn't find it there neither. You can use plain PHP for it, here is one of solutions that can be found for example in this SO question:

$dirs = array_filter(glob('*'), 'is_dir');
print_r($dirs);

For retrieving subdirectories, you can use recursion.

Community
  • 1
  • 1
arogachev
  • 33,150
  • 7
  • 114
  • 117
  • I wonder why they didn't included it! I am going to mark this _accepted_ as it answers my query. – leninhasda Aug 29 '16 at 09:54
  • @leninhasda Probably is not so common task as listing files. Also framework can't cover all such tasks. But remember, you can always post an issue in official Github repo and if you provide enough facts that is demanded and required and contributors agree with you - most likely it will be implemented and included in framework core. – arogachev Aug 29 '16 at 10:32