What are some best practices for handling file deletion in PHP scripts?

When deleting files in PHP scripts, it is important to ensure that the file exists before attempting to delete it. Additionally, it is recommended to check for proper permissions to avoid errors during deletion. Finally, it is good practice to handle any potential errors that may occur during the deletion process.

$file_path = 'path/to/file.txt';

if (file_exists($file_path)) {
    if (is_writable($file_path)) {
        if (unlink($file_path)) {
            echo 'File deleted successfully.';
        } else {
            echo 'Error deleting file.';
        }
    } else {
        echo 'File is not writable.';
    }
} else {
    echo 'File does not exist.';
}