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:
<?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.- 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, butfile_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
Sharing PHP Session ($_Session) Across Multiple Domain
Yii2 Links Between Frontend and Backend (Advanced Template)
How to Convert a Single Array into a Multidimensional Array in PHP
Laravel 5 Carbon Global Locale
How to Safely Store a Password Inside PHP Code
How to Get the First Day of the Week for the Current Locale? (Php/L8N)
PHP String Comparasion to 0 Integer Returns True
How to Replace Some Characters with Asterisks
Symfony: Change Database Dynamically
Error Tolerant HTML/Xml/Sgml Parsing in PHP
Set Cart Item Price from a Hidden Input Field Custom Price in Woocommerce 3
How to Find the Largest Common Substring Between Two Strings in PHP
Ip Address Is Showing in Form Action with Codeigniter Http://::1/Codeigniter/ in HTML Sourcecode