Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从文件中以纯文本形式输出php代码_Php_Html - Fatal编程技术网

从文件中以纯文本形式输出php代码

从文件中以纯文本形式输出php代码,php,html,Php,Html,假设我们有一个template.php文件,其中包含一堆html和一些php代码,例如: ... <body> <div class="content"> <?php echo $content; ?> </div> </body> ... 。。。 ... 我希望将文件内容作为文本(包括php代码)读取,并在文本区域中输出,以便用户能够编辑模板。 我试图使用文件获取内容并使用htmlspecialch

假设我们有一个
template.php
文件,其中包含一堆html和一些php代码,例如:

...
<body>
    <div class="content">
        <?php echo $content; ?>
    </div>
</body>
...
。。。
...
我希望将文件内容作为文本(包括php代码)读取,并在文本区域中输出,以便用户能够编辑模板。
我试图使用
文件获取内容
并使用
htmlspecialchars
呈现它来实现这一点,但它似乎在从文件读取php代码时执行它。在这种情况下,如何避免解释和执行php代码?谷歌搜索没有帮助,也许我还不够努力。有什么建议吗?谢谢

编辑:
好的,我解决了这个问题。问题是我将文件的URL路径传递到
file\u get\u contents
,而不是它的文件系统路径,这显然导致所有php代码被剥离并忽略(?)。感谢您提供的提示,尽管

文件获取内容
不会执行PHP代码,除非您试图在远程服务器上编辑文件并提供URL。在这种情况下,您从远程服务器获得的将是执行的PHP代码,而不是源代码

要么编辑一个本地文件,要么您需要提供一个“后门”,将返回给定PHP文件的代码。小心,因为这是一个安全漏洞-任何人都可能阅读PHP的源代码

避免这种情况的一种方法是,仅当文件位于给定目录中时才接受提供文件。 另一种可能是检查文件内容:

<?php

    // This file will read any local PHP file (or any file of any kind,
    // returning it as text), provided they contain a written agreement
    // to be read.
    if (!isset($_REQUEST['file']))
        die("ERROR: no file supplied");
    $path = $_REQUEST['file'];
    if (!is_readable($path))
        die("ERROR: file is not readable");
    $content = file_get_contents($path);
    if (false === strpos($content, "IT IS OK TO READ THIS FILE AS SOURCE"))
        die("ERROR: this file is not authorized");
    if (false !== strpos($content, "IT IS NOT OK TO READ THIS FILE AS SOURCE"))
        die("ERROR: this file is not authorized");
    Header("Content-Type: text/plain");
    Header("Content-Length: " . strlen($content));
    die($content);
?>

也不能包含相反含义的声明。

文件获取内容
不会执行代码,您能告诉我们您的方法吗?您能告诉我们生成
$content的代码吗?
// IT IS OK TO READ THIS FILE AS SOURCE