0

I have a text file that i would like to display a part of so i use this code:

<?php
$page = file_get_contents('news.txt');
echo $page;
?>

But in news.txt if i have this code <?php echo"hello world!"; ?> It will not display... when i look at the source code of my page i see my code in red. How can i display it.

btw: I know about include but thats not what i need.

Thanks for your help, Sanchixx.

Edited by Sanchixx: a

5
Contributors
17
Replies
21
Views
4 Years
Discussion Span
Last Post by Sanchixx
Featured Replies
  • 1
    diafol 3,669   4 Years Ago

    If user input can reach the eval() function, you could be in trouble. A DW user once told us that he had this eval() function on one of his pages. So, I typed in some code to get a list of all the files in his webroot and I then … Read More

0

What do you want as a result "hello world" or "<?php echo "hello world!"?>"?

0

That was not in my original code, it's a typo. The code works but when i want to display php it doesen't display it. And this is not the code i use i did not want to display a long pagination script so i posted only that.

Edited by Sanchixx: a

0

I want to compile the code in my text file. I'm able to compile php file. But the code in news.txt does not wan't to compile. I was not clear before.

0

If the file that is being 'got' by file_get_contents() has the extension '.txt' will the server parse the file as PHP?
Change '.txt' to '.php' and see if it helps.
If you want to keep the extension as .txt, and still have the file parsed by the server, you could use the include() directive.

Edited by adam.adamski.96155: typo

0

PHP scripts are not compiled, they are interpreted (parsed) by the webserver on the fly as and when the script is called by the broswer.
If you want the PHP in the text file to be parsed (what you would call compiled), then you can use - include() or require() and the extension of the file is irrelevant.
It seems that none of the filesystem functions will parse the PHP code in the file whatever the extension is (.php or .txt)

0

But i would only like to show a part of the file i seperated paarts of my file by a character, then i explode it in an array so i can paginate it: is possible to include a element of an array?

Edited by Sanchixx: sdsd

0

Can't you just use two different files? Is there a reason that two types of data (plain text and PHP code) must be in the same file? From your questions I think you have gone off track and need to think about this again. What exactly do you want to acheive?

0

If the file is php code and you've placed parts of it into an array, I fail to see how you're going to run that code unless you use the eval() function - which you should not. Tempting... no... under no circumstances should you even consider it... well, maybe... no eval is evil!

0

Thank you i will be able to solve my problem :)

Edited by Sanchixx: aa

0

You've asked this question and posted almost no code. I can't understand what you're trying to do. Any chance that you can post more code?

0

I've solved my problem thanks to you but what are the dangers of eval?

1

If user input can reach the eval() function, you could be in trouble.

A DW user once told us that he had this eval() function on one of his pages. So, I typed in some code to get a list of all the files in his webroot and I then pasted my name all over his frontpage. I then posted him a PM with his DB connection details. He got the message and took it off. :)

If you can isolate / secure any input to the function, you may be able to use it safely. But this, as the manual says, really should be a last resort.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.