如何将PHP文件加载到变量中?

时间:2021-11-24 05:38:08

I need to load a PHP file into a variable. Like include();

我需要将PHP文件加载到一个变量中。像包括();

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文件。

8 个解决方案

#1


102  

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');

#2


99  

ob_start();
include "yourfile.php";
$myvar = ob_get_clean();

ob_get_clean()

ob_get_clean()

#3


6  

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脚本与正在运行的脚本位于同一服务器上时,才能读取PHP脚本本身。你可以用类似的东西

$Vdata = file_get_contents('/path/to/your/file.php");

#4


6  

If you want to load the file without running it through the webserver, the following should work.

如果您想要在不通过webserver运行的情况下加载文件,那么应该执行以下操作。

$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 <?php and ?> tags for eval to evaluate it.

这将加载然后计算文件内容。PHP文件需要使用 标签。

#5


4  

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不工作虽然让我感到吃惊的是,当你指定的本地路径....

#6


2  

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.

或者,您可以启动输出缓冲,执行include/require,然后停止缓冲。使用ob_get_contents(),您可以将其他PHP文件输出的内容获取到一个变量中。

#7


1  

file_get_contents() will not work if your server has allow_url_fopen turned off. Most shared web hosts have it turned off by default due to security risks. Also, in PHP6, the allow_url_fopen option 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()将无法工作。由于安全风险,大多数共享web主机默认关闭了它。另外,在PHP6中,allow_url_fopen选项将不再存在,所有函数都将作为默认设置为off。

Your best option to use if you are accessing the file through http is cURL

如果通过http访问文件,最好的选择是cURL

#8


0  

If your file has a return statement like this:

如果您的文件有如下返回语句:

<?php return array(
  'AF' => 'Afeganistão',
  'ZA' => 'África do Sul',
  ...
  'ZW' => 'Zimbabué'
);

You can get this to a variable like this:

你可以把它变成这样的一个变量:

$data = include $filePath;

#1


102  

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');

#2


99  

ob_start();
include "yourfile.php";
$myvar = ob_get_clean();

ob_get_clean()

ob_get_clean()

#3


6  

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脚本与正在运行的脚本位于同一服务器上时,才能读取PHP脚本本身。你可以用类似的东西

$Vdata = file_get_contents('/path/to/your/file.php");

#4


6  

If you want to load the file without running it through the webserver, the following should work.

如果您想要在不通过webserver运行的情况下加载文件,那么应该执行以下操作。

$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 <?php and ?> tags for eval to evaluate it.

这将加载然后计算文件内容。PHP文件需要使用 标签。

#5


4  

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不工作虽然让我感到吃惊的是,当你指定的本地路径....

#6


2  

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.

或者,您可以启动输出缓冲,执行include/require,然后停止缓冲。使用ob_get_contents(),您可以将其他PHP文件输出的内容获取到一个变量中。

#7


1  

file_get_contents() will not work if your server has allow_url_fopen turned off. Most shared web hosts have it turned off by default due to security risks. Also, in PHP6, the allow_url_fopen option 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()将无法工作。由于安全风险,大多数共享web主机默认关闭了它。另外,在PHP6中,allow_url_fopen选项将不再存在,所有函数都将作为默认设置为off。

Your best option to use if you are accessing the file through http is cURL

如果通过http访问文件,最好的选择是cURL

#8


0  

If your file has a return statement like this:

如果您的文件有如下返回语句:

<?php return array(
  'AF' => 'Afeganistão',
  'ZA' => 'África do Sul',
  ...
  'ZW' => 'Zimbabué'
);

You can get this to a variable like this:

你可以把它变成这样的一个变量:

$data = include $filePath;