3

I'm attempting to file_get_contents and output php code as a string without being rendered. The idea is to grab the raw un-rendered file contents so they can be edited in a textarea...

// file "foo.php" I'm needing the contents of
<h1>foo</h1>
<? include 'path/to/another/file' ?>

// php file that's calling the file_get_contents
<?php
    echo file_get_contents('foo.php');
?>

The above code is stripping out the php include in foo.php which outputs:

<h1>foo</h1>

Does anyone know how I can get foo.php contents as a raw un-rendered string where output will be?:

<h1>foo</h1>
<? include 'path/to/another/file' ?>

Any help is greatly appreciated!

6
  • 2
    Have you viewed the resulting source to make sure the content is really gone? Commented Jun 18, 2010 at 19:59
  • What you did should work. The only situation I can think of that would fail is if you are accessing foo.php as an URL (domain/foo.php)... Commented Jun 18, 2010 at 20:01
  • 1
    Doh! Okay, it's showing up in source but not in the DOM?? Commented Jun 18, 2010 at 20:02
  • Victor, yes it's a local file. Commented Jun 18, 2010 at 20:04
  • Charles, is there a method for the php code string to show up in DOM though? Commented Jun 18, 2010 at 20:09

2 Answers 2

10

As far as I know you can't get php content unless it's on the same server. Make sure you're trying to access a locally hosted file and not something remote and it should work.

Also if you try to echo code it will try to parse it, so pass it through htmlspecialchars($source) and it should work.

Something like this:

<?php
    echo "<pre>";
    echo htmlspecialchars(file_get_contents('file.php'));
    echo "</pre>";
?>

Would echo formatted source code of the php file, including comments and any other text in it without being parsed. And since it looks like it's important to you, I'd also say that it shows in the DOM of course since it's no longer code, now it's text. You can place it inside a container, style it and do whatever you want with it.

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

1 Comment

Worked perfectly. Thanks thisMayhem!
2

You can also do :

<?php 

highlight_file('file.php');
// or alternatively
echo highlight_file('file.php',true);

And that will output the file like with htmlspecialchars and file_get_content but within <code> tags and with some syntax highlighting.

highlight_string :

(PHP 4, PHP 5, PHP 7)

highlight_string — Syntax highlighting of a string

highlight_file :

(PHP 4, PHP 5, PHP 7)

highlight_file — Syntax highlighting of a file

2 Comments

The answer from 2010 was a more complete, better answer. I don't think your answer really solves the exact problem the OP is looking for. See Juan Cortes' answer above.
@benny I was thinking "I'm attempting to file_get_contents and output php code as a string without being rendered" was exactly the point of the highlight instruction, and would be a useful tool for future users.

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.