$dir = FCPATH."uploads/attachment/";
$this->getFiles($dir);

function getFiles($dir)
{
$arr=array();
if (is_dir($dir))
{
$data = scandir($dir . $file);
foreach ($data as $file) {
if ($file != '.' && $file != '..') {
if (is_dir($dir.$file)) {
$this->getFiles($dir.$file.'/');
} else {
if (strtolower(strrchr($file, '.')) == '.txt') {
//echo $dir.$file. PHP_EOL;
$arr[]=$file;
}
}
}
}
}
else{
}
}