简体   繁体   English

如何从file_get_contents运行php代码

[英]How to run php code from file_get_contents

I have a file template.php : 我有一个文件template.php

<html>
    <head>
        <title>{title}</title>
    </head>
    <body>
    <div id="header">
            <h1>This is header</h1> 
        </div>
        <div id="content">
            <h1>This is content</h1>
            <?php $this->showContent(); ?>
        </div>
    <div id="footer">
        <p>Copyright &copy; <?= date('Y'); ?> by My Company.</p>
    </div>
    </body>
</html>

and in file controll.php , code look like this: 在文件controll.php中 ,代码如下所示:

$file = file_get_contents('template.php');
echo $file;

but it not run php script $this->showContent(); 但它不会运行php脚本$this->showContent(); .

Can somebody help me? 有人可以帮我吗?

you should try 你应该试试

require_once('template.php'); // will execute the page as php

What file_get_contents will do is read the file as some kind of textfile and return the value of it. file_get_contents要做的是将文件读取为某种文本文件并返回其值。 That's not what you want. 那不是你想要的。 require_once.php will include the php file and handle it as php. require_once.php将包括php文件并将其作为php处理。 This would be similar as having the code in the file you are including it in. 这类似于将代码包含在其中的代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM