How to Run PHP Code from File_Get_Contents or File in a Function

How to run php code from file_get_contents or file in a function

You could execute the php code and collect the output like this:

ob_start();
include "template.phtml";
$out1 = ob_get_clean();

http://www.php.net/manual/de/function.ob-get-contents.php

PHP file_get_contents(file.php); execute PHP code in file

To execute the code before you get the contents, you either need to include and capture the output:

ob_start();
include('file.php');
$content = ob_get_clean();

Or get the file by URL (probably not the best idea and not portable):

$content = file_get_contents('http://example.com/file.php');

Do file_get_contents and readfile execute PHP code?

file_get_contents and readfile do not execute code. All they do is return the raw contents of the file. That could be text, PHP code, binary (e.g. image files), or anything else. No interpretation of the files' contents is happening at all.

The only situation in which it may appear as if execution is happening is:

  1. <?php ?> tags will likely be hidden by the browser because it's trying to interpret them as HTML tags, so this may lead to the impression that the PHP disappeared and hence may have been executed.
  2. You're reading from a source which executes the code, e.g. when reading from http://example.com/foo.php. In this case the functions have the same effect as visiting those URLs in a web browser: the serving web server is executing the PHP code and returning the result, but file_get_contents merely gets that result and returns it.

How to output interpreted PHP-code from file_get_contents() of a PHP file?

Instead of using file_get_contents(), use include():

<?php include($source); ?>

This will actually run the code through the PHP interpreter, rather than just printing its text-contents inside the page.

file_get_contents returns PHP code

You can use the files url (not filepath), so it is processed by the server eg:

echo file_get_contents('http://website.com/test.php');

However include/require would be better, eg:

include 'test.php';

File_get_contents with PHP to retrieve JSON output of code as opposed to source code when file is on same server

The direct route would be to use a URL instead of a file path so that the data is fetched over HTTP and the web server generates the response to the URL by executing the PHP program.

The better approach would be to refactor the code into a function, include the file, then call the function.

mylib.php

function getJSON() {
return json_encode(array('item'=>$dataobject));
}

myfile.php

include("mylib.php");
header("Content-Type: application/json");
echo getJSON();

other.php

include("mylib.php");
$object = getJSON();

file_get_contents and php code

You can use an output buffer to include a PHP file but save the resulting output for later use instead of printing it immediately to the browser.

ob_start();
include('path/to/file.php');
$output = ob_get_contents();
ob_end_clean();

// now do something with $output

PHP: file_get_contents a PHP file with include();

file_get_contents() will get the content of a file, not execute it as a PHP script. If you want this piece of code to be executed, you need to either include it, or process it (through an HTTP request to Apache, for instance).

If you include this file, it'll be processed as PHP code, and of course, print your HTML tags (include* can take any kind of file).

If you need to work with its content before you print it, use ob_* functions to manipulate the PHP output buffer. See : https://www.php.net/manual/en/ref.outcontrol.php

ob_start(); // Start output buffer capture.
include("yourtemplate.php"); // Include your template.
$output = ob_get_contents(); // This contains the output of yourtemplate.php
// Manipulate $output...
ob_end_clean(); // Clear the buffer.
echo $output; // Print everything.

By the way, such mechanism sounds heavy for a template engine. Basically, templates should not contain PHP code. If you want such behavior, have a look at Twig : http://twig.sensiolabs.org/

Hiding php codes from file_get_contents method

You have to run the template file, not simply read it.

You can do that like this:

ob_start();
include ( $template_file );
$mail_template = ob_get_clean();

ob_start() (output-buffer start) causes all php script output (from echo etc) to go into an output buffer. output_get_clean() retrieves that output.



Related Topics



Leave a reply



Submit