I'm trying to open a text file and output its contents with the code below. The text file includes line breaks but when I echo the file its unformatted. How do I fix this?
Thanks.
<html>
<head>
</head>
<body>
$fh = fopen("filename.txt", 'r');
$pageText = fread($fh, 25000);
echo $pageText;
</body>
</html>
Are you outputting to HTML or plain text? If HTML try adding a
<br>
at the end of each line. e.g.Say you have an
index.php
file hosted by the web server. You want to insert some multi-line text file contents into it. That's how you do it:This
<?= ... ?>
part is just a shorthand, which instructs the web server, that it needs to be treated as a PHPecho
argument.For simple reads like this, I'd do something like this:
This will take care of carriage return and output the text. Unless I'm writing to a file, I don't use fopen and related functions.
Hope this helps.