Question

I would like to match file name from directory which I know the starting portion of the file along with underscore. The format of the files will be always mp3. For example the file name 12_title.mp3 should read to a variable if I pass 12. In the directory the starting numbers are unique

any help is appreciated thank you in advance

Was it helpful?

Solution

Try the scandir with preg_grep will help

$myfiles=scandir("files/");  
$id="12";  
$matches=preg_grep("/".$id."_.+mp3$/i",$myfiles);
foreach($matches as $key=>$keyvalue)
{
$file=$keyvalue;
echo "Matched filename = ".$file;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top