0

I have the following code that I am using to list all the files in a specific directory which I then return as JSON

<?php
header('Content-Type: application/json');
$vars = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );

$id = $vars['id'];

$dir = $_SERVER['DOCUMENT_ROOT'] . '/resources/' . $id;

$list = array(); //main array

if(is_dir($dir)){
if($dh = opendir($dir)){
    while(($file = readdir($dh)) != false){

        if($file == "." or $file == ".."){
            //...
        } else { //create object with two fields

            array_push($list, $file);
        }
    }
}


echo json_encode($list);
}
?>

However, as well as returning the files, it is also returning a subdirectory that is also in that folder. How do change the if statement to also check whether it is an actual file that has been retrieved?

DigM
  • 509
  • 1
  • 4
  • 24

1 Answers1

0

Meet is_dir()

if ($file == "." or $file == ".." or is_dir($file)) {
Mitya
  • 33,629
  • 9
  • 60
  • 107