Why does echo only display the first line when using fopen to read a file in PHP?
When using fopen to read a file in PHP, the file pointer is positioned at the beginning of the file. If you try to use echo to display the contents of the file without moving the file pointer, it will only display the first line. To display the entire contents of the file, you need to use a loop to read and output each line until the end of the file is reached.
$file = fopen("example.txt", "r");
if ($file) {
while (!feof($file)) {
echo fgets($file);
}
fclose($file);
} else {
echo "Unable to open file.";
}