What are potential pitfalls when using the file_get_contents() function in PHP to read data from a file?
Potential pitfalls when using the file_get_contents() function in PHP include not handling errors properly, not checking if the file exists before attempting to read it, and not considering security implications when reading files from external sources. To avoid these issues, it's important to check if the file exists, handle errors gracefully, and sanitize input to prevent security vulnerabilities.
$file_path = 'path/to/file.txt';
if (file_exists($file_path)) {
$file_content = file_get_contents($file_path);
if ($file_content !== false) {
// Process file content here
} else {
echo "Error reading file.";
}
} else {
echo "File does not exist.";
}