Page 1 of 1

load php script from file

Posted: Mon Dec 12, 2005 4:04 am
by righteous
I'm trying to load a file 'test.php' into a variable.

file test.php:
<?php
print("Hello");
?>

when i try to load the file:

$tmp = file_get_contents('test.php');

i get an empty variable.

Is it possible to get a php content from a file?

Posted: Mon Dec 12, 2005 5:37 am
by onion2k
There's a few ways of doing it. You should probably investigate the include() function first.

RPY

Posted: Mon Dec 12, 2005 7:09 am
by righteous
I'm familiared with include(), require(),... but this is not what i want, because i don't want a compiled code.

I just want to read a php script from a file. But if i read a php script from a file with standard file handling functions i can only read a non-php content.
Everything between (<?PHP ?>) just disappeare.

Posted: Mon Dec 12, 2005 7:23 am
by AGISB
you might need to use entities for the tags

Posted: Mon Dec 12, 2005 7:43 am
by righteous
Success!

After a little search i found a way. Function file_get_contents(file) gave me a desired result. :D

Example:
temp.php:
$code = file_get_contents("temp1.php");
$src = file_get_contents("temp2.html");
$ret = str_replace('%SECOND%', $code, $src);
$ret = '?>'.$ret;
eval($ret);

temp1.php:
<STRONG>
<?PHP
print("second");
?></STRONG>

temp2.html:
First<BR>
%SECOND%<BR>
Third.<BR>

The result of the script is:
First
Second
Third

thank you! :D :D :D

Posted: Mon Dec 12, 2005 11:42 am
by Jenk

Code: Select all

/*ignore this line, it is simply in place to correct the syntax highlighting on the forums*/?>

First<BR>
<?php include("temp1.php"); ?><BR>
Third.<BR>
Would do exactly the same job.