Jump to content

[SOLVED] Printing a text file to a page?


quarantine

Recommended Posts

I am trying to figure out how to print a text file line by line on a HTML page when it loads.

 

This is the basic setup of the page I have .....

 

<html>

<head>


<title>Guest Book </title>

</head>

<body>
<form action="guestbook.php" method="post">
Name:<input type="text" name="name" />
<br />
Message:<input type="text" name="body" />

<input type="submit" value="Sign Guest Book" />
</form> 

<br />

<br />

<?php

// set file to read
$file = 'guestbook.txt' or die('Could not read file!');
// read file into array
$data = file($file) or die('Could not read file!');
// loop through array and print each line
foreach ($data as $line) {
     echo $line;
}

?> 

</body>

</html>

 

I tried using foreach() and reading the text file line by line, but when I go to the html page nothing happens. Am I putting this php in the wrong place? How can I best archive this?

 

*EDIT* I would like to text file to appear line by line, instead of printing all on one line.

Link to comment
https://forums.phpfreaks.com/topic/155089-solved-printing-a-text-file-to-a-page/
Share on other sites

 

Thanks, that works great, but it is printing all on the same line like......

 

texttextextextextext

 

I was trying to get it to print line by line like....

 

text
text
text
text

 

Is there a way to do that?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.