load php script from file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
righteous
Forum Newbie
Posts: 3
Joined: Mon Dec 12, 2005 3:48 am

load php script from file

Post 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?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

There's a few ways of doing it. You should probably investigate the include() function first.
righteous
Forum Newbie
Posts: 3
Joined: Mon Dec 12, 2005 3:48 am

RPY

Post 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.
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

you might need to use entities for the tags
righteous
Forum Newbie
Posts: 3
Joined: Mon Dec 12, 2005 3:48 am

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
Post Reply