Merge / include PHP files with PHP? - php

In my local environment I have many small PHP files in different folders. I use "include" or "require once" to run them together.
I'm going to upload my little project to the web but I only want to upload it as one PHP file. I need to merge the files together.
I found a nice function that makes an include as a string.
function get_include_contents($filename)
{
if (is_file($filename)) {
ob_start();
if(file_exists($filename)) include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
It gives me the HTML result, not the PHP code. I need to get the PHP code.

function get_include_contents($filename)
{
if (is_file($filename) && is_readable($filename)) {
return file_get_contents($filename);
}
return false;
}

include is in effect running the code within $filename. Instead, you should use file_get_contents(), which will return the raw contents of the file, without parsing them.

Related

Is is possible to get file output buffer without including the file?

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

How to get multiple PHP files output into a single a file for further usage of those output?

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;
}
?>

(PHP) How to pass the previous INCLUDE / REQUIRE calls into child files?

I'm creating my own Templating Script by using the ob_get_contents() as a core method. By using it, it can render out the other files, calling from the one single file.
Just like, lets assume we have 4 files:
index.php
header.html
footer.html
functions.php
index.php will call and render the contents of other files (2 html files here). By using the following codes:
//index.php
function render($file) {
if (file_exists($file)) {
ob_start();
include($file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
}
echo render('header.html');
echo render('footer.html');
But (for example) when header.html contains a call include('functions.php'), the included file (functions.php) can't be used again in footer.html. I mean, i have to make a include again in footer.html. So here, the line include('functions.php') have to be containing in both of files.
How to include() a file without calling it again from child files?
When you use ob_start() (Output buffering), you end up only with the output of the file, meaning file executed the output is returned by ob_get_content(). As only output is returned that other file is unaware of the includes.
So the answer is: you can't do it with output buffering. Or include your files before ob_start with include_once.
That could work like something like this:
//index.php
function render($file) {
if(!isset($GLOBALS['included'])) {
$GLOBALS['included'] = array();
}
if (!in_array($file, $GLOBALS['included']) && file_exists($file)) {
ob_start();
include($file);
$content = ob_get_contents();
ob_end_clean();
$GLOBALS['included'][] = $file;
return $content;
}
}
echo render('header.html');
echo render('footer.html');
Alternatively you could use include_once (include_once $file;)and PHP will do it for you.
Though I suggest you just make sure the file loading structure is in such shape that these events never happen.

PHP/Perl filesize function not working on new server

I have a problem with a function that doesn't work as expected since I have moved my site from a shared hosting to a VPS (both have the same Linux OS, php version 5.2.9 and Perl version 5.8.8).
When my script store a remote file into a local directory, I run a simple php script at regular intervals (5 seconds) using XMLHttpRequest, this php script execute a Perl script that return the current file size (bytes already downloaded).
Here is the php code:
<?php
if (isset($_GET['file'])) {
clearstatcache();
$file = $_GET['file'];
exec("/usr/bin/perl /home/xxxxxx/public_html/cgi-bin/filesize.pl $file", $output);
//print_r($output);
if (!empty($output) || $output[0] != "") {
$currentSize = $output[0];
file_put_contents('progress.txt', $currentSize);
} else {
...
...
}
}
?>
Here is the Perl code
#!/usr/bin/perl
$filename = $ARGV[0];
$filepath = '/home/xxxxxx/public_html/tmp_dir/'.$filename.'.flv';
$filesize = -s $filepath;
print $filesize;
When I was running these scripts on the shared server, I had no problem and could see the download progress, but now, the file size is only printed when the remote file has been fully downloaded and I can't see the progress.
I think I need to change something in the php settings but I'm not sure and I don't know what needs to be changed.
OK, I'm sorry/stupid, the filesize() function works fine, thank you all guys.
If you need the file size, you could also just call the filesize function from PHP, and avoid having to use perl altogether.
The problem is probably caused by a different file location. Are you positive that the file '/home/xxxxxx/public_html/tmp_dir/'.$filename.'.flv' exists? You could test it with:
if (-e '/home/xxxxxx/public_html/tmp_dir/'.$filename.'.flv')
Remember that you could use PHP filesize() instead:
<?php
if (isset($_GET['file'])) {
clearstatcache();
$file = $_GET['file'];
if (file_exists("/home/xxxxxx/public_html/tmp_dir/$file.flv") {
$currentSize = filesize("/home/xxxxxx/public_html/tmp_dir/$file.flv");
file_put_contents('progress.txt', $currentSize);
} else {
...
...
}
}
?>

Set included PHP and HTML content to a variable

I am working on a small MVC "framework", which will simply provide a more organized workspace for me, as I've found I love CakePHP but only for it's organization. Now, I'm trying to replicate the layouts (which contain <?php echo $title_for_layout; ?> and <?php echo $content_for_layout; ?>,) and I'm curious how I would be able to load a mixed PHP and HTML file... save what would normally be outputted to the browser to a variable... set the variables mentioned inside the variable, and then output the layout variable and have PHP use the variables inside it to setup the layout exactly how I want it.
Any ideas?
Capture the buffer output to a string with the output buffer control functions.
$string = getIncludeContents('template.php');
function getIncludeContents($filename) {
if (is_file($filename)) {
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
Example taken from.

Categories