How can the contents of a directory be deleted before deleting the directory itself in PHP?

To delete the contents of a directory before deleting the directory itself in PHP, you can use the `scandir()` function to get a list of files in the directory, loop through the list and delete each file using `unlink()`. After deleting all the files, you can then use `rmdir()` to delete the directory itself.

$dir = 'path/to/directory';

$files = scandir($dir);
foreach ($files as $file) {
    if ($file != '.' && $file != '..') {
        unlink($dir . '/' . $file);
    }
}

rmdir($dir);