What best practice can be implemented to prevent only the last file in a directory from being deleted when using 'unlink' in PHP?

To prevent only the last file in a directory from being deleted when using 'unlink' in PHP, you can first check if there is more than one file in the directory before deleting the file. If there is only one file left, you can choose to not delete it to ensure that at least one file remains in the directory.

$directory = "/path/to/directory/";

$files = glob($directory . "*");
if (count($files) > 1) {
    unlink($files[0]); // Delete the first file in the directory
}