What is the recommended method in PHP to delete the contents of a folder that contains multiple subfolders filled with text files?
To delete the contents of a folder that contains multiple subfolders filled with text files in PHP, you can use a recursive function to iterate through all the files and folders within the main folder and delete them one by one. This ensures that all files and subfolders are removed before deleting the main folder itself.
function deleteFolderContents($dir) {
$files = glob($dir . '/*');
foreach ($files as $file) {
if (is_dir($file)) {
deleteFolderContents($file);
rmdir($file);
} else {
unlink($file);
}
}
}
$folderPath = 'path/to/your/folder';
deleteFolderContents($folderPath);
rmdir($folderPath);