Help!! Text from text file to output to a webpage

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
de2k6
Forum Newbie
Posts: 1
Joined: Mon Nov 27, 2006 2:53 am

Help!! Text from text file to output to a webpage

Post by de2k6 »

Hello I'am a noob at php and im planning to get the text from a textfile and then output it in a webpage? What command should I use please help tnx
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Code: Select all

echo file_get_contents($filename);
Follow this link file_get_contents

Note:You may want to perform some checks on the filename to ensure it is valid.
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

including

Post by Jaxolotl »

Hi CoderGoblin, for wich reason you choose to use the file_get_contents() function with the consequence of making a resource instead of using include() or include_once() with the advantage of having the possibility of writing php code on the txt file and still can parse it when the file_get_contents() convert the content into a string?

Is there a security reason behind your choose?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Must admit include would probably work better... That will teach me not to think an answer through.

In my defence however, :wink: I used to build the page details as a variable before output (I am playing at the moment with mvc and Zend Framework). The reasoning behind this approach was mainly to avoid header redirect problems and avoiding mixing the PHP section and direct output. As a result the file would look like....

Code: Select all

<?php
$page='';
...
...
$page.=file_get_contents($filename);
...
...
...
?>
<html>
  <head>
  </head>
  <body>
    <?php echo $page; ?>
  </body>
</html>
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

defense

Post by Jaxolotl »

Dear CoderGoblin you don't need to defend your position brother, I'm here to learn...like everyone I supose... many time we use a preferred function because we like it, but someone may show as a security failure we didn't consider or see before, I was just asking because including outsource my have undesiderable consequences if we don't pray attention and I consider myself a learn long way walker ;)
TheProgrammer
Forum Newbie
Posts: 22
Joined: Mon Nov 27, 2006 12:25 am

Post by TheProgrammer »

One more notice from me:

take a look at what are you outputing. if it's html code use either of the methods used before. probably include would work best. but if it's not html code, you should probably use the file_get_contents function along with htmlentities in order to render it right. if you would have characters like < it would probably screw things up. a trim wouldn't hurt also. use it like this:

Code: Select all

echo htmlentities( trim( file_get_contents ("file_path") ) );
Ain't I right guys? :)
Post Reply