Pinkie Pinkie - 16 days ago 5
PHP Question

PHP: How to list files in a directory without listing subdirectories

This is the starting portion of my code to list files in a directory:

$files = scandir($dir);
$array = array();
foreach($files as $file)
{
if($file != '.' && $file != '..' && !is_dir($file)){
....


I'm trying to list all files in a directory without listing subfolders. The code is working, but showing both files and folders. I added
!is_dir($file)
as you see in my code above, but the results are still the same.

Answer

It should be like this, I think:

$files = scandir($dir); 
foreach($files as $file)
{
    if(is_file($dir.$file)){
      ....