How can PHP be used to read directory structures and files?

To read directory structures and files in PHP, you can use the opendir() function to open a directory handle, readdir() function to read the directory contents, and is_dir() function to check if an entry is a directory. You can also use file_get_contents() function to read file contents.

// Open a directory handle
$dir = opendir('path/to/directory');

// Read directory contents
while (($file = readdir($dir)) !== false) {
    if ($file != "." && $file != "..") {
        // Check if entry is a directory
        if (is_dir($file)) {
            echo "Directory: $file\n";
        } else {
            // Read file contents
            $content = file_get_contents($file);
            echo "File: $file - Content: $content\n";
        }
    }
}

// Close directory handle
closedir($dir);