Joakim
Joakim

Reputation: 113

getting data from php variable

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

echo $content;

nothing displays, expect when displaying the page sourcecode in browser the display is this

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

so it wont execute the script when getting the content..

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

and if I do next

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

it complains about unexpected foreach...

is there a way to read the folder/.php files content to single $variable and then echo/print all folder/.php files to page where it should be?

thanks for help already.

Upvotes: 0

Views: 97

Answers (2)

DaveyBoy
DaveyBoy

Reputation: 2915

What you're trying won't execute the contents of the "file.php", jsut display the contents of them on screen.

If you want to execute file.php, use eval ($content)

To capture the output, use something like:

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

Upvotes: 1

mamadrood
mamadrood

Reputation: 757

Is that what you want to do ?

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

Upvotes: 1

Related Questions