What potential pitfalls should be considered when using the unlink() function in PHP to delete a file?
When using the unlink() function in PHP to delete a file, potential pitfalls to consider include accidentally deleting important files, lack of error handling which can lead to unexpected behavior, and potential security vulnerabilities if user input is not properly sanitized.
$filename = "file_to_delete.txt";
if (file_exists($filename)) {
if (unlink($filename)) {
echo "File deleted successfully.";
} else {
echo "Error deleting file.";
}
} else {
echo "File does not exist.";
}