Search code examples
phpregexglob

How to find a file name inside directoryphp


I woul try to develop a function to find an element of file name inside a directory.

Is it possible ? name of file : apps_module_name.json

search request could be for example : a or apps or name

I think the glob function must be use ? I tried this

%$file_name% : is wrong but is something like mysql search than I am looking.

 $result = glob(OSCOM::getConfig('dir_root', 'Shop') . $this->ModuleInfosJson . '/' . %$file_name . '.json');

Solution

  • Asterisk * matches all. Not %. Correct code will be.

    $result = glob(OSCOM::getConfig('dir_root', 'Shop') . $this->ModuleInfosJson . '/*' . $file_name . '*.json');
    

    It matches AnythingFilenameAnything.json

    Eg:

    glob("a*.php") will match

    a.php 
    aa.php 
    ab.php  
    abc.php