106

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

I have loaded a simple HTML file like this:

$Vdata = file_get_contents("textfile.txt");

But now I need to load a PHP file.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500
Kombuwa
  • 1,613
  • 4
  • 20
  • 35

8 Answers8

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

ob_get_clean()

lowtechsun
  • 1,915
  • 5
  • 27
  • 55
neobie
  • 2,847
  • 5
  • 27
  • 31
123

I suppose you want to get the content generated by PHP, if so use:

$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:

$Vdata = file_get_contents('path/to/YOUR/FILE.php');
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • 6
    what if i want to get " the content generated by PHP " without using http and directly use the path ? – Osa Sep 10 '12 at 09:27
  • 4
    @Osa: Depending on your needs, you could probably use `eval()` or if you're dealing with more complicated code: https://github.com/nikic/PHP-Parser. – Alix Axel Sep 10 '12 at 09:42
  • 2
    implementing a php parser just for that seems like a huge overkill – David Fariña Aug 29 '13 at 11:44
  • 1
    I don't see how this will pass the POST or GET. I think neobie's answer would be more correct and appropriate, since the external PHP file would be processed in the context of the current one. – ADTC Nov 19 '16 at 16:33
  • Using the example above you can use get_template_directory_uri() and join to your file path. This will solve the issue of using a direct path. `$Vdata = file_get_contents(get_template_directory_uri() . '/path/to/file/in/theme.text');` – Ian Bruce Feb 24 '20 at 17:58
  • but if I would like to use variables which were created in the controller and get the data into a variable I would need to use the `ob_start(); include "yourfile.php"; $myvar = ob_get_clean();` [down below](https://stackoverflow.com/a/7052882/6901693), because both of these ways do not result in what I would expect – Utmost Creator Nov 07 '20 at 17:52
16

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;
António Almeida
  • 9,620
  • 8
  • 59
  • 66
9

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

$Vdata = file_get_contents('/path/to/your/file.php");
Jonathan Weiss
  • 664
  • 1
  • 6
  • 9
  • hi this only get static html. see this example. if this is the file we are going to load; ///////////////// Text2 /////////////// this is the out put i got; //////////////////// Text2 /////////////////// – Kombuwa Aug 13 '09 at 14:34
  • @Kombuwa, could you, please, clarify your response in an edit to your original question, where you have more space to clearly explain? Thanks =) – David Thomas Aug 13 '09 at 14:39
6

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

Jess
  • 42,368
  • 6
  • 37
  • 51
4

Theoretically you could just use fopen, then use 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....

Zeroshade
  • 463
  • 2
  • 8
3

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.

Alex Weinstein
  • 9,823
  • 9
  • 42
  • 59
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.

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

Mark
  • 6,254
  • 1
  • 32
  • 31