ลองเอาฟังชั่น ไปประยุคใช้ดูก็ได้ครับ

function getFileRecursive($path, $pattern = '', $recursive = true){
$file_list = array();
if(!file_exists($path)){
print $path." doesn't exist";
return;
// throw new Exception($path." doesn't exist");
}
$dir_list = array();
if ($handle = opendir($path)) {
/* This is the code to loop over the directory. */
while (false !== ($file = readdir($handle))) {
if(preg_match('/^\./', $file)) continue;
if ( !is_dir($path.'/'.$file) ) {
if(!empty($pattern) && !preg_match($pattern, $file)) continue;
$file_list[] = array(
'file_name' => $file ,
'file_path' => $path.'/'.$file ,
);
} elseif($recursive) {
$dir_list = array_merge(getFileRecursive($path."/".$file, $pattern), $dir_list);
}
}
}
$file_list = array_merge($file_list, $dir_list);
return $file_list;
}