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

When deleting files in PHP, it is important to check if the file exists before attempting to delete it to avoid errors. It is also recommended to use absolute file paths to ensure the correct file is being deleted. Additionally, it is good practice to handle any potential errors that may occur during the deletion process.

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

if (file_exists($filename)) {
    if (unlink($filename)) {
        echo 'File deleted successfully';
    } else {
        echo 'Error deleting file';
    }
} else {
    echo 'File does not exist';
}