包含 - 使用字符串而不是文件名

Tem*_*345 3 php string eval include

我想在字符串而不是文件上运行include,但是不知道如何实现这一点.

//This is the desired functionality
include($filename);

//But I want to do something like this instead.
$file_contents = getFileFromCacheOrSomewhereElse($filename);
include($file_contents);  // Doens't work...
eval($file_contents);     // Also incorrect.
Run Code Online (Sandbox Code Playgroud)

请注意:"eval"与include不同 - "include"回显文件内容(并执行任何PHP标记),而"eval"将字符串作为PHP代码执行.

一个示例用例是从Memcache加载模板文件(作为字符串),然后在该字符串上运行include,而不是运行include并依赖于PHP filecache.

sal*_*the 10

如果可以转动onallow_url_fopenallow_url_includephp.ini设置,则一个替代是data流包装(手册).

include 'data:text/plain,' . urlencode($file_contents);
Run Code Online (Sandbox Code Playgroud)


Mor*_*oth 7

eval("?>" . $file_contents . "<?php ");
Run Code Online (Sandbox Code Playgroud)

可以.