3

I want to echo out the php code itself for educational purposes. Is this possible? At the same time I also need the code as php code to be executed.

adding html special-chars to this:

$this->art = file_get_contents("$this->Mainpage/$this->dir/$this->article");

it becomes:

$this->Art = htmlspecialchars( file_get_contents("$this->Mainpage/$this->dir/$this->Artikel"));

But this still does not output the code itself as the php interpreter seems to get hold of the code and directly interprets it. htmlspecialchars () has only got an effect on the html code, you then get to see the article in brackets.

I also tried using .html files instead of .php files. But PHP interprets it even if you rename it to .jpeg.

I'm at my wit's end. I would be grateful for any answer. Thanking in advance.

6
  • 1
    file_get_contents doesn't interpret any PHP, there's something else going on here. What's actually in the file in question? Commented Jun 13, 2015 at 14:30
  • How do you actually echo the code? Commented Jun 13, 2015 at 14:30
  • simply do this str_replace('<?php','',$yourdata); Commented Jun 13, 2015 at 14:31
  • $this->Art = file_get_contents("$this->Mainpage/$this->dir/$this->Artikel"); $this->section = explode ("End_Embed", $this->Art); $this->Meta_Text=$this->section[0]; $this->content=$this->section[1];echo ("$this->content"); Commented Jun 13, 2015 at 14:38
  • but I'll try Feroz's code. Thank you. Just trying to grasp what is going on inside the skin of php as it were. Commented Jun 13, 2015 at 14:42

2 Answers 2

3

I dont understand, why one would actually consider doing it this way, but it is pretty easy. file_get_content will not run the code, while include will.

define('DS', DIRECTORY_SEPARATOR);

// Logic starts here
public function main() {
    echo '<hr><pre>' . $this->educlude($this->Mainpage . DS. $this->dir . DS . $this->Artikle) . '</pre>';
}

public function educlude($file) {
    include $file;
    return htmlspecialchars(file_get_contents($file));
}

This will echo

Hello World


<?php echo 'Hello World';

for given file:

<?php echo 'Hello World';

Sign up to request clarification or add additional context in comments.

Comments

0

'echo' seems to somehow process php-code, even if it is a txt file. So before echoing I did this, the precondition being that the code is saved as .txt:

$this->Art =nl2br (htmlspecialchars(file_get_contents("$this->Mainpage/$this->dir/$this->Artikel"))); 

if you do this with a file ending with .php the code seems to be even processed by 'file_get_contents' So you have to use .txt.

1 Comment

It does not. You are probably downloading a file (file_get_contents works for local and remote files), so your webserver preprocesses the php code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.