How to get names of all files in a directory with PHP

Mar 12, 2012   //   by phpfreelancer.biz   //   Blog, PHP Tutorial  //  No Comments

I want to get all names of all the files in a directory in PHP.

$log_directory = 'your_dir_name_here';

$results_array = array();

if (is_dir($log_directory))
{
if ($handle = opendir($log_directory))
{
//Notice the parentheses I added:
while(($file = readdir($handle)) !== FALSE)
{
$results_array[] = $file;
}
closedir($handle);
}
}

//Output findings
foreach($results_array as $value)
{
echo $value . '
';
}

Leave a comment

Share This Post

RSS Wordpress News