PHP Get file name starting with prefix

 $target_file_png = glob($target_dir.'/group_'.$groupId.'*.png'); 

$target_file_png will return an array containing all the files in folder specified in the path $target_dir starting with '/group_'.$groupId.' and specify the file format as *.png


You need to change the regular expression in preg_grep:

$files = preg_grep('~^tpl-.*\.php$~', scandir(admin . "templates/default/"));

Explanation:

  1. ^tpl- - starting with "tpl-"

  2. .* - any characters

  3. \.php$ - ending with ".php"


I like another, simple way:

1. get all files in folder

   $path = './images'; 
   $files = glob($path.'/*');

2. get all files having extension .jpg

   $path = './images'; 
   $files = glob($path.'/*.jpg');

3. get all files having prefix myprefix_

   $path = './images'; 
   $files = glob($path.'/myprefix_*');

Tags:

Php