Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include from "php://memory" stream

I'm writing a system for a browser application that will store some particular php scripts in a database and then pull them out and execute them when needed. At first I tried using exec() and piping to php the output of a script that got the scripts out of the database and printed them. This worked in one use case, but not all, and feels brittle anyway, so I'm looking for a better way.

I'm now attempting to accomplish this through use of a PHP file stream in memory. For instance:

$thing = <<<'TEST'
<?php

$thing = array();

print "Testing code in here.";
var_dump($thing);

?>
TEST;

$filename = "php://memory";

$fp = fopen($filename, "w+b");
fwrite($fp, $thing);
//rewind($fp);

fclose($fp);

include "php://memory";

However, nothing is printed when the script is executed. Is this even possible by this means, and if not, is there another way to do this? I'm trying to avoid having to write temporary files and read from them, as I'm sure accessing the filesystem would slow things down. Is there a URL I can provide to "include" so that it will read the memory stream as if it were a file?

I don't think eval() would do this, as, if I remember correctly, it's limited to a single line.

Also, please no "eval = include = hell" answers. Non-admin users do not have access to write the scripts stored in the database, I know that this needs special treatment over the life-cycle of my application.

like image 864
terminal_case Avatar asked Mar 30 '12 14:03

terminal_case


1 Answers

You need to use stream_get_contents to read from the php://memory stream. You cannot include it directly.

like image 71
pHiL Avatar answered Nov 15 '22 16:11

pHiL