What are the best practices for handling user permissions in PHP file deletion scripts?

When handling user permissions in PHP file deletion scripts, it is important to ensure that only authorized users have the ability to delete files. One way to do this is by checking the user's permissions before allowing the deletion to proceed. This can be done by verifying the user's credentials or roles against a list of authorized users.

// Check user permissions before deleting file
if (/* Check if user has permission to delete files */) {
    $fileToDelete = 'example.txt';

    if (file_exists($fileToDelete)) {
        unlink($fileToDelete);
        echo 'File deleted successfully';
    } else {
        echo 'File not found';
    }
} else {
    echo 'You do not have permission to delete files';
}