1

I am just doing an excersise in php. I`ve started my lamp server, created the html file and send it to /opt/lampp/htdocs ( saw that in phpinfo() ). Here is the tip.html content:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> TIP OF THE DAY</title>
</head>

    <!-- start the body -->
    <body>
    <h1>Tip of the day</h1>
    <?php 
        print "<h3> Here is your tip:</h3>";
    ?>
    <div style="border-color:green; border-style: groove; border-width: 2px;">
    <?php
    readFile("tips.txt");
    ?>
    </div>
    </body>
</html>

When I open localhost/tip.html it gaves me a partial output... Here is your tip:"; ?> kind of partially parsing the output from the php script tag??? Any info why is that?

Ilian Zapryanov
  • 1,132
  • 2
  • 16
  • 28

2 Answers2

3

Try viewing source. You'll see the entire content of the file is being served, without the <?php ?> tags being evaluated. If you want PHP to handle serving a file, you need to either name the file with a .php extension (tip.php, in this case), or configure your web server to handle .html files through PHP.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • @meagar... hm.. weird... I`ve jsut removed/installed again it via pacman and seemed that fixed the issue... But I`ve installed php on the past friday. OK... this ssolves the issue, I guess. – Ilian Zapryanov Feb 02 '12 at 18:33
0

It looks you don't need readFile but file_get_contents() to put the output in that location in the HTML file.

<?php echo file_get_contents("tips.txt"); ?>
Luc Franken
  • 2,994
  • 1
  • 17
  • 14
  • nope... it`s not related.. readFile woked now after I reinstalled php, apache, lampp... everything actually. – Ilian Zapryanov Feb 02 '12 at 18:35
  • Just tested it, correct and seems to work. General use is file_get_contents. Normally readfile is being used for binary files because it doesn't store the contents of the file in a string. Whatever option you choose: Check whether the file could be loaded when you start using this in production environment so you don't show ugly errors (with possibly path information) when the command fails or the text file is not available. – Luc Franken Feb 02 '12 at 18:41