What are the best practices for handling file deletion tasks in PHP applications?

When handling file deletion tasks in PHP applications, it is important to ensure that the file exists before attempting to delete it. Additionally, it is good practice to check if the file is writable and to handle any errors that may occur during the deletion process.

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

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