0

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.

4
  • If you want to merge them just copy and paste them by hand. If it's too long to do that, then it's probably a good thing to keep the files separated. Commented Dec 30, 2010 at 15:23
  • As a side note, generally, please don't do this. There's little benefit and it makes maintenance less pleasant if someone else has to work with the code. It's especially bad if PHP files in those subfolders have their own relatively pathed include calls. Commented Dec 30, 2010 at 15:23
  • This is weired, you can just tar-ball / zip archive entire directories structure, and upload into whatever server ... of course, un-tar / unzip later on Commented Dec 30, 2010 at 15:40
  • I got your point BUT... I have alot of "template" files and depending on my own settings, only some of them are used. I don't want to give away my whole combination system for this, just the combined result. Commented Dec 30, 2010 at 15:49

2 Answers 2

2
function get_include_contents($filename)
{
    if (is_file($filename) && is_readable($filename)) {
        return file_get_contents($filename);
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.