从php变量获取数据


getting data from php variable

$content=file_get_contents('file.php');

echo$content;

什么都不显示,除了在浏览器中显示页面源代码时,显示的是这个

<? foreach(glob("folder/*.php") as $class_filename) { require_once($class_filename); } ?>

因此它在获取内容时不会执行脚本。。

file.php包含以下代码<? foreach(glob("folder/*.php") as $class_filename) { require_once($class_filename); } ?>

如果我做下一次

$content = foreach(glob("folder/*.php") as $class_filename) { require_once($class_filename); } ?>

它抱怨意外的前臂。。。

有没有一种方法可以将文件夹/.php文件的内容读取到单个$variable中,然后将所有文件夹/.php文件回声/打印到应该位于的页面?

谢谢你的帮助。

这就是你想要做的吗?

$content = '';
foreach (glob('folder/*.php') as $class){$content .= file_get_contents($class);}
echo $content;

您尝试的不会执行"file.php"的内容,jsut会在屏幕上显示它们的内容。

如果要执行file.php,请使用eval ($content)

要获取输出,请使用以下内容:

ob_start();              // Don't echo anything but buffer it up
$codeToRun=file_get_contents('file.php'); // Get the contents of file.php
eval ($codeToRun);       // Run the contents of file.php
$content=ob_get_flush(); // Dump anything that should have been echoed to a variable and stop buffering
echo $content;           //echo the stuff that should have been echoed above