0

I am using PHP 7.4.1.

I am trying to get files from a certain folder and I use the following code for this:

array_diff(scandir($views . "/Text"), array('.', '..'));

I have the following folder structure:

Texts/
├── test1.blade.php
└── Parts

test1.blade.php is a file and Parts is a folder.

The above code gives me back files AND folders.

I would only like to get all .blade.php files in my array.

Any suggestions how to filter out the folder(s)?

Appreciate your replies!

Carol.Kar
  • 3,775
  • 32
  • 114
  • 229
  • 1
    Iterate and check with `is_dir` for example. – u_mulder Nov 22 '20 at 10:08
  • 1
    See here: https://stackoverflow.com/questions/15774669/list-all-files-in-one-directory-php – AbsoluteBeginner Nov 22 '20 at 10:13
  • 1
    Does this answer your question? [Getting the names of all files in a directory with PHP](https://stackoverflow.com/questions/2922954/getting-the-names-of-all-files-in-a-directory-with-php) – Adam P. Nov 22 '20 at 10:20

1 Answers1

3
  • $result = array_filter($result, 'is_file'); - files
  • $result = array_filter($result, 'is_dir'); - folders
WinterSilence
  • 312
  • 2
  • 14
  • Thx for your reply! How can I use your `array_filter` function with my `array_diff(scandir($views . "/Text"), array('.', '..'));` when passing this as an array I get an error: `array_filter('is_file',array_diff(scandir($views . "/Text"), array('.', '..')));` – Carol.Kar Nov 22 '20 at 10:29
  • ups.. my fail, switch arguments: `array_filter($result, 'is_file')` – WinterSilence Nov 22 '20 at 12:01
  • also u can use array_diff() but array_filter() remove root links in any case – WinterSilence Nov 22 '20 at 12:05