Help!! Text from text file to output to a webpage
Moderator: General Moderators
Help!! Text from text file to output to a webpage
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
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
Code: Select all
echo file_get_contents($filename);Note:You may want to perform some checks on the filename to ensure it is valid.
- Jaxolotl
- Forum Contributor
- Posts: 137
- Joined: Mon Nov 13, 2006 4:19 am
- Location: Argentina and Italy
including
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?
Is there a security reason behind your choose?
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
Must admit include would probably work better... That will teach me not to think an answer through.
In my defence however,
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....
In my defence however,
Code: Select all
<?php
$page='';
...
...
$page.=file_get_contents($filename);
...
...
...
?>
<html>
<head>
</head>
<body>
<?php echo $page; ?>
</body>
</html>- Jaxolotl
- Forum Contributor
- Posts: 137
- Joined: Mon Nov 13, 2006 4:19 am
- Location: Argentina and Italy
defense
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
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:
Ain't I right guys? 
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") ) );