tags:

views:

52

answers:

2

I need to read a file in PHP, but I only know the end of the filename, so I need to serch the file in a given directory using wildcards: *filename.wav and then return it to the browser. Is there a function to do that? or I need to get all the files in the directory and then search one by one?

Thanks for all the comments and help.

+5  A: 

Take a look at the glob() function

For example if you wanted to get every file matching *abc.xyz in the subdirectory dir

$matches = glob('./dir/*abc.xyz');

Note that glob isn't recursive and will only search a single directory and not all sub directories.

Yacoby
+1 btw, hey, nice unicorn! It looks angry. The ominous gleam makes it even more terrifying.
Pekka
@Pekka I am mainly disappointed in the lack of a visible rainbow :(
Yacoby
True. If only one could swap avatars, mine's got more rainbow than I need! :)
Pekka
+3  A: 

In addition to Yacoby's answer, I would like to add that a new GlobIterator class has been added to PHP >= 5.3.

It allows one to use wildcards, the same way as glob() does -- but, in additional :

  • Tt implements the Iterator interface
  • You can have it work returning instance of the SplFileInfo class


So many useful things in the SPL (Standard PHP Library), and no-one ever thinking about them... I couldn't resist.

Pascal MARTIN