Jump to content

Reading a text file and displaying its contents


Mike D

Recommended Posts

So, I'm trying to get this read my text document "news.txt" and display the contents.

 

My code:

div.newscontent{
padding:0px;
margin-top:60px;
margin-bottom:0px;
margin-left:400px;
margin-right:0px;
position:absolute;
width:457px;
height:330px;
text-align:center;
color:#FFFFFF;	
}

 


<div class='newscontent'>
<?PHP

$filename = "news.txt";
$arr = file($filename);
foreach($arr as $line){
    print $line . "<br/>";
}

?>
</div>

 

This is the output I get:

"; } ?> 

 

Whats wrong? :confused:

Either your file is not named *.php or PHP is not installed/installed properly.

 

How did you come to that conclusion?

Experience, and from having seen many of these questions in my time.

 

Look at the code as HTML markup, not PHP code: the <?php is the beginning of a tag which doesn't end until the next />. The following

"; } ?>

will be rendered as text - precisely as OP described.

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.