How do I load a PHP file into a variable?

PhpFile

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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

ob_get_clean()

Solution 2 - Php

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

Solution 3 - Php

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;

Solution 4 - Php

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

Solution 5 - Php

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.

Solution 6 - Php

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....

Solution 7 - Php

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.

Solution 8 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKombuwaView Question on Stackoverflow
Solution 1 - PhpneobieView Answer on Stackoverflow
Solution 2 - PhpAlix AxelView Answer on Stackoverflow
Solution 3 - PhpAntónio AlmeidaView Answer on Stackoverflow
Solution 4 - PhpJonathan WeissView Answer on Stackoverflow
Solution 5 - PhpJessView Answer on Stackoverflow
Solution 6 - PhpZeroshadeView Answer on Stackoverflow
Solution 7 - PhpAlex WeinsteinView Answer on Stackoverflow
Solution 8 - PhpMarkView Answer on Stackoverflow