$f, "type" => "folder", "path" => $dir . '/' . $f, "items" => scan($dir . '/' . $f) // Recursively get the contents of the folder ); } } else if (is_file($dir . '/' . $f)) { // It is a file $files[] = array( "name" => $f, "type" => "file", "path" => $dir . '/' . $f, "size" => filesize($dir . '/' . $f) // Gets the size of this file ); } } } return $files; } // Output the directory listing as JSON header('Content-type: application/json'); echo json_encode(array( "name" => basename($dir), "type" => "folder", "path" => $dir, "items" => $response ));