2012-04-03 73 views
0

$ content = file_get_contents('file.php');從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); } ?> 

它抱怨意想不到的foreach ...

有沒有辦法讀取文件夾/ .php文件內容,以單個$變量,然後回聲/打印所有文件夾/ .php文件到頁面,它應該是?

感謝您的幫助。

+0

你能打印你得到確切的錯誤信息? – 2012-04-03 15:34:56

回答

1

這就是你想要做的嗎?

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

其實是的.. 但這兩個答案都很好,我可以使用它們。 :) 謝謝 – Joakim 2012-04-03 18:23:32

+0

不,DaveyBoy代碼更好,如果你想執行**的PHP代碼。 Mine不會執行文件中的代碼,它只會讀取文件內容。所以,如果你需要執行你的文件,使用DaveyBoy代碼,如果你不需要它,我會更好(因爲你不需要評估文件)。這是兩種不同的用法。 – mamadrood 2012-04-03 19:28:29

1

你正在嘗試不會執行「file.php」的內容,只是在屏幕上顯示它們的內容。

如果你想執行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 
+0

感謝您的代碼:) – Joakim 2012-04-03 18:28:50