PHP Get File Contents and merge

Hi all,

I am using this code:

$Mainsource = ('mainFile.php');

$MainContent = file_get_contents($Mainsource, true);

Open in new window


Now, can something like this be done?

$header = ('header.php');
$Mainsource = ('mainFile.php');

$MainHeader = file_get_contents($header, true);
$MainContent = file_get_contents($Mainsource, true);

$MergedContent = $MainHeader + $MainContent;

Open in new window


Thanks



error77Asked:
Who is Participating?
 
Beverley PortlockCommented:
Why not just use include?

include 'header.php';
include 'mainFile.php';

Open in new window

0
 
ncooCommented:
Yes that can be done, but the output ($MergedContent) will only be valid depending on the inputs.

If you were to do this for images or videos for example this method would probably not give you what you expect.
0
 
Ray PaseurCommented:
To give you a good answer we would need to see the contents of the data in these elements:

$header = ('header.php');
$Mainsource = ('mainFile.php');

Please check your level of PHP and see example #2 on the man page.
http://php.net/manual/en/function.file-get-contents.php

Please post the files or post URLs that link to those files and we will be able to tell you how to handle them.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
error77Author Commented:
The contents of the data is pure html and php code...nothing else.

0
 
Ray PaseurCommented:
@Brian: I was wondering that myself.  But I didn't know what error77 wanted to do with the $MergedContent variable :-)
0
 
error77Author Commented:
Looking into it...
0
 
error77Author Commented:
OK...I'm still using this code:

$Mainsource = ('mainFile.php');

$MainContent = file_get_contents($Mainsource, true);

mainFile.php is already written and doesn't need changing so yes I could have used an include BUT I need to add 1 line of code dynamically to the top of the file "mainFile.php"...

So, support mainFile.php content was:

<?php
   echo 'This is the main content';
?>

I need to add 1 line of code BEFORE that content and before it is saved so,,,

I'm adding

<?php $userid="10"; ?>

so this final "mainFile.php" file when saved will look like:


<?php $userid="10"; ?>
<?php
   echo 'This is the main content';
?>

So, I'm getting all that code together and saving it all to a new file.

The part I need to know is:

How do I mearge the 2 codes together.

Hope this helps

THanks
0
 
Ray PaseurCommented:
When a programmer is asked for something like, "Please post the files or post URLs that link to those files and we will be able to tell you how to handle them." and the response is, "The contents of the data is pure html and php code...nothing else." there is some kind of fundamental disconnect.  We may be able to help, but not if we cannot see what is created from your files.

I'm thinking that you might benefit from some of these learning resources in PHP.
http://us3.php.net/tut.php
http://www.sitepoint.com/books/phpmysql4/

For better or worse, computer programming is an activity that requires the highest level of precision.  Even in some cases, you must have case-sensitive information (in PHP the variable names are case-sensitive).  So when we ask for specific information and we get vaguely generalized responses, it is a sign that our questions are not well understood.  For that reason I am suggesting a trip through the introductory materials.  I hope it helps.  Best regards, ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.