How can the readdir function be used to loop through directories and subdirectories in PHP?
To loop through directories and subdirectories in PHP, the readdir function can be used in conjunction with recursive function calls. By recursively calling the function for each subdirectory found, we can iterate through all directories and their contents.
function listFiles($dir){
$files = array_diff(scandir($dir), array('..', '.'));
foreach($files as $file){
if(is_dir($dir.'/'.$file)){
listFiles($dir.'/'.$file);
} else {
echo $dir.'/'.$file . "\n";
}
}
}
// Start the loop from the root directory
listFiles('/path/to/directory');