如何将 PHP 文件加载到变量中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1272228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I load a PHP file into a variable?
提问by Kombuwa
I need to load a PHP file into a variable. Like include();
我需要将一个 PHP 文件加载到一个变量中。喜欢include();
I have loaded a simple HTML file like this:
我加载了一个简单的 HTML 文件,如下所示:
$Vdata = file_get_contents("textfile.txt");
But now I need to load a PHP file.
但现在我需要加载一个 PHP 文件。
回答by neobie
回答by Alix Axel
I suppose you want to get the content generated by PHP, if so use:
我想您想获取PHP 生成的内容,如果是这样,请使用:
$Vdata = file_get_contents('http://YOUR_HOST/YOUR/FILE.php');
Otherwise if you want to get the source code of the PHP file, it's the same as a .txt file:
否则如果你想获取PHP 文件的源代码,它和 .txt 文件是一样的:
$Vdata = file_get_contents('path/to/YOUR/FILE.php');
回答by Jess
If you want to load the file without running it through the webserver, the following should work.
如果你想加载文件而不通过网络服务器运行它,以下应该工作。
$string = eval(file_get_contents("file.php"));
This will load then evaluate the file contents. The PHP file will need to be fully formed with <?phpand ?>tags for evalto evaluate it.
这将加载然后评估文件内容。PHP文件需要与完全形成<?php并且?>标签eval对其进行评估。
回答by Jonathan Weiss
If you are using http://, as eyze suggested, you will only be able to read the ouput of the PHP script. You can only read the PHP script itself if it is on the same server as your running script. You could then use something like
如果您使用http://,正如 eyze 建议的那样,您将只能读取 PHP 脚本的输出。如果 PHP 脚本与您正在运行的脚本在同一台服务器上,则您只能读取它本身。然后你可以使用类似的东西
$Vdata = file_get_contents('/path/to/your/file.php");
回答by Zeroshade
Theoretically you could just use fopen, then use stream_get_contents.
理论上你可以只使用 fopen,然后使用 stream_get_contents。
$stream = fopen("file.php","r");
$string = stream_get_contents($stream);
fclose($stream);
That should read the entire file into $string for you, and should not evaluate it. Though I'm surprised that file_get_contents didn't work when you specified the local path....
这应该为您将整个文件读入 $string ,并且不应该对其进行评估。虽然我很惊讶 file_get_contents 在您指定本地路径时不起作用....
回答by Alex Weinstein
Alternatively, you can start output buffering, do an include/require, and then stop buffering. With ob_get_contents(), you can just get the stuff that was outputted by that other PHP file into a variable.
或者,您可以启动输出缓冲,执行包含/要求,然后停止缓冲。使用 ob_get_contents(),您可以将其他 PHP 文件输出的内容放入变量中。
回答by António Almeida
If your file has a return statement like this:
如果您的文件有这样的 return 语句:
<?php return array(
'AF' => 'Afeganist?o',
'ZA' => 'áfrica do Sul',
...
'ZW' => 'Zimbabué'
);
You can get this to a variable like this:
您可以将其设置为这样的变量:
$data = include $filePath;
回答by Mark
file_get_contents() will not work if your server has allow_url_fopenturned off. Most shared web hosts have it turned off by default due to security risks. Also, in PHP6, the allow_url_fopenoption will no longer exist and all functions will act as if it is permenantly set to off. So this is a very bad method to use.
如果您的服务器已关闭allow_url_fopen,file_get_contents() 将不起作用。由于安全风险,大多数共享网络主机在默认情况下都将其关闭。此外,在PHP6 中,allow_url_fopen选项将不再存在,所有功能都将像永久设置为 off 一样。所以这是一个非常糟糕的使用方法。
Your best option to use if you are accessing the file through http is cURL
如果您通过 http 访问文件,最好的选择是 cURL

