Page 1 of 1

read php script from memory

Posted: Wed Jul 21, 2010 3:56 pm
by julianb
I saw a php function which uses "php://temp" to read and write a "file" in memory. I wanted to write a bunch of PHP code to memory and then read it back from memory. Can I use php://temp to do so?

Code: Select all

$somecode = "<? someFunction();
function someFunction(){
echo \"you successfully ran someFunction\";
} ?" . ">";
this works (using "shm" memory in Linux, not MS-Windows compatible):

Code: Select all

file_put_contents("/dev/shm/1.php", $somecode);
$fp = fopen('php://temp', 'r+');
include '/dev/shm/1.php';
this works: (using hard drive)

Code: Select all

file_put_contents("1.php", $somecode);
$fp = fopen('php://temp', 'r+');
include '1.php';
doesn't work:

Code: Select all

$allFunctions=fopen('php://temp', 'r+');fwrite($allFunctions,$array);rewind($allFunctions);//fclose($allFunctions);
include 'php://temp';
I am testing the code using PHP 5.2.

Re: read php script from memory

Posted: Wed Jul 21, 2010 4:33 pm
by Weirdan
you can't use php://temp (or php://memory) for that because every time it's accessed it creates a new 'file'. So when you're running include("php://temp") new (empty) memory segment is created, distinct from what you used before.

However you can use eval(stream_get_contents($fp)); combo to achieve similar effect.

Re: read php script from memory

Posted: Wed Jul 21, 2010 4:39 pm
by julianb
Just found eval() doing a google search.

That is just what I was looking for.