Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

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



0
error77
Asked:
error77
2 Solutions
 
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
 
error77Author Commented:
The contents of the data is pure html and php code...nothing else.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Beverley PortlockCommented:
Why not just use include?

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

Open in new window

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now