0

$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.

1
  • Could you print the error messages you're getting exactly? Commented Apr 3, 2012 at 15:34

2 Answers 2

1

Is that what you want to do ?

$content = '';
foreach (glob('folder/*.php') as $class){$content .= file_get_contents($class);}
echo $content;
Sign up to request clarification or add additional context in comments.

2 Comments

actually yes.. but both answers are good and I can use them. :) thanks
No, DaveyBoy code is better if you want to execute the php code. Mine will not execute the code in the file, it will just read the file content. So if you need to execute your files, use DaveyBoy code, if you don't need it mine is better (because you don't eval files). This is two different usage.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.