Is it possible to get file output buffer string to execute the file in background instead of including it?
Right now this is the only way which I have seen. See code below;
ob_start();
include($file);
$html = ob_get_contents();
ob_end_clean();
But I have list of files which I don't want to include. I just want to execute the file in background process for getting output buffer string.
Can anyone please help me on it?
Thanks
Smac
If your file is php you need to use include.For multiple files you can use function like this I found here
function include_multi($files) {
$files = func_get_args();
foreach($files as $file)
include($file);
}
And call it with
include_multi("one.php", "two.php", ..);
Also If you want plain file contents(No need to execute file) you can use
file_get_contents or readfile
Related
Good Day All
I have a .php file which I want to edit via fopen() and file_get_content() functions. However, my file contains some php codes as well and I managed to get the content out of my file but without the php part. Also, I have tried the eval() (I know it's not suggested!) function with same results. I was wondering if there could be a way to get whatever is inside that file regardless wether it's text or codes.
Thanks
Here is the code I used:
public function editwarning()
{
$filename = "http://www.parkho.ir/admin/templates/pm/email_warning.php";
$content = file_get_contents($filename);
echo $content;
}
You have two options:
1) pass the file PATH to the $filename var:
$filename = "/var/www/app/email_warning.php"; // <--- replace /var/www/app for your path
2) Or You need to use htmlentities():
<?php
$content = htmlentities(file_get_contents($filename));
echo $data;
script1.php returns results:
helen,hunt
jessica,alba
script2.php returns results:
bradley,cooper
brad,pitt
script.php looks like this
<?php
echo "name,surname";
require_once('script1.php');
require_once('script2.php');
?>
and will return
name,surname
helen,hunt
jessica,alba
bradley,cooper
brad,pitt
Question
Maybe this is obvious to someone, but I am struggling for a while how to save this php file: script.php as csv file? script1.php and script2.php both produce result over while loop, so I would be able to save results as array inside of a while loop however hopefully someone will offer an easy solution to my original question.
If I get it right, you are trying to save the output of those scripts to a CSV file.
Try doing:
ob_start();
echo "name,surname";
require_once('script1.php');
require_once('script2.php');
$result = ob_get_contents();
file_put_contents('filename.csv', $result)
You can also take a look at fputcsv: http://php.net/manual/en/function.fputcsv.php
If your question is how to do it having the access to the terminal, then just execute it
php -f script.php > file.csv
If you want to do it from the script, you can use output buffering. It will be along the lines of:
<?php
ob_start();
echo "name,surname";
require_once('script1.php');
require_once('script2.php');
$size=ob_get_length();
if ($size > 0)
{
$content = ob_get_contents();
// save $content to file here
ob_clean();
}
?>
Or, alternatively, if you can change script1.php and script2.php, make them open the file for appending (fopen('fname.csv', 'a')), and write to that file, it will result in rows from both of them written to the file without overwriting each other.
I am trying to get the output of multiple PHP files into a single PHP file where I can save them into different variables for further usage.
For a single PHP file, I used include and it works well. But for multiple files I don't know what to do.
Do you have any experiences or advices on how to achieve this ?
I had three php files called a.php,b.php and c.php. Now in each file i am echoing an array as output. For first php file i done like below to save the output of that in fourth php file called d.php
ob_start();
include_once('a.php');
$output= ob_get_clean();
echo "<pre>";print_r($output);
Now what to do for getting second and third php files outputs.
I think this is what you want
<?php
$string1 = get_include_contents('somefile1.php');
$string2 = get_include_contents('somefile2.php');
function get_include_contents($filename) {
if (is_file($filename)) {
ob_start();
include $filename;
return ob_get_clean();
}
return false;
}
?>
Let's say i have a file consisting of 5 lines of text and every line has 50 chars. The output buffer contents are returned correctly but if I have a file containing 100 lines of text the output buffer returns empty string (string with value null).
I do it like so:
ob_start();
include "file.php"
$string = ob_get_contents();
ob_end_clean();
OR
$string = $this->load->view('view', $data, true);
Im doing this inside codeigniter if that makes any difference.
I tried to load the file with Code igniter's load->view function with third parameter set to true the result is the same. Tried also giving ob_start() a big number -> ob_start(9999999); same result;
I'd propose to use ob_get_flush to ensure, that nothing is still kept in some internal buffer..
Quite unlikely, but what does this instead of your code print?
require_once( "file.php" );
Just to ensure, that the stuff in file.php isn't surrounded by <?php /** **/ php?>.
And what does
echo ob_get_level();
output just before your code? Shouldn't be relevant, if other outbut-buffering is already enabled, but...
Why are you using ob functions to get the contents of a file? Why not file_get_contents?
you have just to add some lines in index.php (the root of CodeIgniter)
ob_start();
/*
*---------------------------------------------------------------
* APPLICATION ENVIRONMENT
*------------
+
require_once BASEPATH.'core/CodeIgniter.php';
$data = ob_get_contents();
ob_clean();
echo $data; /// or anything else
that's all !
I've been writing a php/html page encoder/decoder... I know it already exists but it's a university project so go on XDDD
I encode the pages that I want to protect let's say hypothetically with base64_encode and when I receive a request of any pages I have a loader that reads the coded page, decrypts it and with eval executes it. The real problems arise when I try to decrypt and execute a mixed php/html page. Obviously eval can't execute html code so my question is do I really become crazy about splitting the page executing the php code and print the html? And also if I include an encoded php or php/html page do I really have to reuse the method up here?
I hope someone can really help me because i have a week left before the deadline and I can't change the project at this point.
chris here the function and the fisrt calling in $param[0] i've got the filename called
function MyInclude($filename)
{
// create the temp file
$temp_filename = "tmp.php";
$handle = fopen($temp_filename , 'w+');
if (!$handle)
die('Error creating temp file');
// write the decrypted data, close the handle
$tmp=file_get_contents($filename);
$data=MCrypt_Decode($tmp,'PFL_EPU_V100_mia');
fwrite($handle,$data );
fclose($handle);
// start output buffering to contain any output the script creates
ob_start();
try {
include($temp_filename);
} catch (Exception $e) {
die('There was an error in the encrypted file, cannot process');
}
// get the output, clear the buffer
$output = ob_get_contents();
ob_end_clean();
//destroy the temp file
unlink($temp_filename);
// now you can output the buffer, if desired:
echo $output;
}
MyInclude($param[0]);
the $param[0] file here
<?php
session_start();
$_SESSION['title']='Home';
MyInclude("header.php");
?>
<body>
sono il body <?php echo APP_PATH; ?>
</body>
<?
echo "boss";
MyInclude("footer.php");
?>
any idea about it??? or you need some other code??? let me know T_T
Mike
You can eval() a string that contains mixed html and php, just so long as the tags are included.
http://php.net/manual/en/function.eval.php
When eval() encounters a php close tag (?>), it will stop trying to treat it as php code and just echo everything out until it comes to a php open tag.
The typical solution to your problem is something like this:
$file = ... //Your decoded php/html code here
$file = '?>' . $file; //Add a close tag to the beginning;
ob_start();
eval($file);
$output = ob_get_clean();
echo $output; //Or do something else with it... really, if you're
//just going to be echoing it you can skip the output buffering
Is it possible to decrypt the page, write it to a file, then include it? That would let the PHP interpreter do what it does best - interpret PHP documents. That will include HTML/PHP combinations without relying on eval.
The outline of that would be:
// create the temp file
$temp_filename = "tmp.php";
$handle = fopen($filename , 'w');
if (!$handle)
die('Error creating temp file');
// write the decrypted data, close the handle
fwrite($handle, $decrypted_data);
fclose($handle);
// start output buffering to contain any output the script creates
ob_start();
try {
include_once($temp_filename);
} catch (Exception $e) {
die('There was an error in the encrypted file, cannot process');
}
// get the output, clear the buffer
$output = ob_get_contents();
ob_end_clean();
//destroy the temp file
unlink($temp_filename);
// now you can output the buffer, if desired:
echo $output;
Function references
fopen: http://us2.php.net/manual/en/function.fopen.php
fwrite: http://us2.php.net/manual/en/function.fwrite.php
fclose: http://us2.php.net/manual/en/function.fclose.php
ob_start: http://us2.php.net/manual/en/function.ob-start.php
ob_get_contents: http://us2.php.net/manual/en/function.ob-get-contents.php
ob_end_clean: http://us2.php.net/manual/en/function.ob-end-clean.php
unlink: http://www.php.net/manual/en/function.unlink.php
You will need dump the decoded file to another file and include(); it. The eval approach will not work because it will exit with a parse error if the first item in the file is not either an opening <?php tag, or a valid bit of PHP code.
More than this, you will need to find/replace any occurences of include(), require(), include_once(), and require_once() within the encrypted file with a different function, to ensure you don't try to execute another encrypted file before it has been decrypted. You could do this at execution (ie decryption) time, but it would be much better to it a encryption time, to minimise the time required to pre-fetch the code before it is executed.
You can define these customised functions to decrypt a file and include/require it in your loader script.
Your problem description is a bit vague however your problem seems to be solvable with output buffering.
Have you tried decrypting the page, then parsing the text to split out anything between and then only executing that code?