EnexoOnoma
EnexoOnoma

Reputation: 8836

How to put the files of a folder in an array?

In a folder "images" I have a thousand xml files.Those filenames I want them to be insert into an array $images = array('','');

Instead of writing them all by hand, and this folder will be updated often, how can I do it automatically ?

Upvotes: 2

Views: 121

Answers (2)

Ilmari Karonen
Ilmari Karonen

Reputation: 50328

Just exclude the . and .. entries if you don't want them:

$files = array_diff( scandir($dir), array('.','..') );

Upvotes: 2

Marc B
Marc B

Reputation: 360572

. and .. are always present in ALL directories ('current directory' and 'parent directory', respectively). You have to specifically filter them out. However, since you want only images, you could use something like glob() to just fetch images using regular shell wildcard patterns, e.g.

$files = glob('*.jpg');

which would give you all the files whose names end in .jpg.

Upvotes: 1

Related Questions