Page 1 of 1

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

Posted: Mon Nov 27, 2006 3:04 am
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

Posted: Mon Nov 27, 2006 4:31 am
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.

including

Posted: Mon Nov 27, 2006 4:40 am
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?

Posted: Mon Nov 27, 2006 5:14 am
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>

defense

Posted: Mon Nov 27, 2006 5:27 am
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 ;)

Posted: Mon Nov 27, 2006 5:33 am
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? :)