61

Is there a way to write the PHP file_exists function so that it searches a directory for a file with an arbitrary extension. For instance, suppose I knew that a file were called "hello", but I didn't know the extension, how would I write a function that searched for a file called hello.* and returned the name of this file? As far as I can tell, file_exists will only search for a string.

Thanks.

bsamek
  • 1,773
  • 3
  • 17
  • 23

5 Answers5

109

You're looking for the glob() function.

file_exists doesn't do any kind of search : it only allows one to know whether a file exists or not, when knowing its name.

And, with PHP >= 5.3, you could use the new GlobIterator.


As an example with `glob()`, the following portion of code :
$list = glob('temp*.php');
var_dump($list);

Gives me this output :

array
  0 => string 'temp-2.php' (length=10)
  1 => string 'temp.php' (length=8)

While this one :
$list = glob('te*-*');
var_dump($list);

Yes, with two * ;-)

Will give me :

array
  0 => string 'temp-2.php' (length=10)
  1 => string 'test-1.php' (length=10)
  2 => string 'test-curl.php' (length=13)
  3 => string 'test-phing-1' (length=12)
  4 => string 'test-phpdoc' (length=11)
clami219
  • 2,958
  • 1
  • 31
  • 45
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • I knew there must have been a better function for this. Thank you! – bsamek Apr 30 '10 at 17:32
  • Also note that you're not restricted to `*`; single characters (`?`) and classes (`[abc]`) to name a few can also be put to good use in glob patterns. – salathe Apr 30 '10 at 18:07
  • Just a small addition: the glob() function gets its name from the globbing used in Linux shells (like Bash). https://linuxhint.com/bash_globbing_tutorial/ – Pianoman May 27 '23 at 19:44
9

As of PHP5.3, you can also use the GlobIterator to search a directory with wildcards:

$it = iterator_to_array(
    new GlobIterator('/some/path/*.pdf', GlobIterator::CURRENT_AS_PATHNAME) );

would return the full paths to all .pdf files in some/path in an array. The above performs about the same as glob(), but iterators provide a more powerful and extensible API.

Gordon
  • 312,688
  • 75
  • 539
  • 559
6

As long as file_exists returns a BOOLEAN I wrote this small function that accepts a search string with * to look for files... Example:

    searchForFile("temp*");

    function searchForFile($fileToSearchFor){
        $numberOfFiles = count(glob($fileToSearchFor));
        return ($numberOfFiles > 0);
    }
Rodrigo
  • 4,706
  • 6
  • 51
  • 94
luciomonter
  • 338
  • 5
  • 7
1

If you need a little more control and are on pre PHP 5.3 you could use a DirectoryIterator or RecursiveDirectoryIterator. Both have a lot of great function for added control and manipulation.

PHP docs are at DirectoryIterator and RecursiveDirectoryIterator

Adam Gotterer
  • 598
  • 5
  • 19
0

You can use fnmatch to match one filename against a pattern. Use this in a loop over all files.

if (fnmatch("hello*", $filename)) {
  echo 'true';
}
Moradnejad
  • 3,466
  • 2
  • 30
  • 52