开发者

php: file_get_contents is stripping out php code

开发者 https://www.devze.com 2023-01-04 04:03 出处:网络
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...

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!


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消