Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How to search a file using wildcards

Tags:

file

php

search

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.

like image 278
doc Avatar asked Apr 01 '10 17:04

doc


People also ask

Which wildcards are used for searching for files?

The asterisk ( * ) Use it when searching for documents or files for which you have only partial names. For most web search engines, wildcards increase the number of your search results.

How do you use wildcards to search files and folders?

You can use your own wildcards to limit search results. You can use a question mark (?) as a wildcard for a single character and an asterisk (*) as a wildcard for any number of characters. For example, *. pdf would return only files with the PDF extension.

What is a wildcard search?

Wildcards take the place of one or more characters in a search term. A question mark (?) is used for single character searching. An asterisk (*) is used for multiple character searching. Wildcards are used to search for alternate spellings and variations on a root word.


1 Answers

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.

like image 56
Yacoby Avatar answered Nov 14 '22 04:11

Yacoby