You should use glob() instead of scandir() here, it saves memory and eliminates the need of an extra preg_match.