$directory = '/path/to/directory'; if (is_dir($directory)) { if (!rmdir($directory)) { echo "Failed to delete directory"; } } else { echo "Directory does not exist"; }
$directory = '/path/to/directory'; $files = scandir($directory); foreach ($files as $file) { if ($file !== '.' && $file !== '..') { unlink($directory . '/' . $file); } } if (!rmdir($directory)) { echo "Failed to delete directory"; }In this example, we first use the `scandir()` function to get a list of all files in the directory. We then loop through the list of files and delete them using the `unlink()` function. Once all files have been deleted, we attempt to remove the directory using the `rmdir()` function. The `rmdir()` function is included in the PHP core, so no extra packages or libraries are required.