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