Search code examples
phpcontent-management-systemdomdocumentread-write

Add content to PHP file with PHP / edit PHP file with PHP


I'm building my own CMS system and I want to create new pages dynamicly from a template. Just like in wordpress when you add a new Page.

This is the template:

<?php require_once('backend-nav.php');?>

<div id="main">

<div id="main-content" class="xlarge">

    <article id="article-wrapper">
    // My content needs to go here!
    </article>
<?php require_once('backend-sidebar.php')?>

</div>
</div><!-- End main content container -->

<?php require_once('backend-footer.php')?>

<?php } else {
    echo '<div class="container">You have to be logged in to view this  page:.
'<ahref="login.php">Login</a>'.'</div>';
}
?>

I have made a form to submit the content that I want on the page, and then use the following code to open the templatet php file and save it as a new file on the server with the content for the page

$doc = new DOMDocument();
$doc->loadHTMLFile("new_page.php");
$article = $doc->getElementById('article-wrapper');
$p = $doc->createElement('p');
$addP = $article->appendChild($p);
$content = $doc->createTextNode($page_content);
$addP->appendChild($content);
// the url for the page is also submitted to the form and later added to the menu, which works.
$doc->saveHTMLFile($page_url.'.php');

Since there is no loadPHP function I'd recon I use this one. It also worked for me when adding the link to my main menu, which is also a PHP file.

Now the content gets added to the file, and is saved accordingly but for some reason it fucks up the code in some places like this, some sign are replaced like ? and > etc.:

require_once('backend-header.php');
?>
<?php if (isset($_COOKIE['username'])) { ?>
    <?php require_once('backend-nav.php');?>

<html>
<body>
<div id="main">

<div id="main-content" class="xlarge">

    <article id="article-wrapper">

<p>test content added in p tags</p></article>
<?php require_once('backend-sidebar.php')?>    </div><!-- End main content container -->
</div>

<?php require_once('backend-footer.php')?><?php } else {
    echo '<div class="container">You have to be logged in to view this page: ' . 
    '<a href="login.php">Login</a>'.'';
}?&gt;</body></html>

the PHP ending tag before the html end tag is replaced I have also tried fread/write to alter the file but I probably are not using things the right way.

Is there a way to add code to php file with php, or a other way to get what I'm trying to do?

Thanks!


Solution

  • DOMDocument only use to read XML and HTML, these have a structure. When you insert PHP code into html file, it is not realy a html anymore. Let see an example below.

    The html code:

    <a>text</a>
    

    There is a node that named "a" have a content. DOMDocument can understand it well. But

    <a><?php if (false) : ?>true</a><? else: ?>false</a><?php endif ?>
    

    DOMDocument can not understand php and it will read the first < /a> as the closer of < a>. How about the second one, the reader may try to read by fixing it or just ignore it or append something to make it become structured. So, you can not use DOMDocument in this case. You could try to use file_get_contents and replace the content then use file_put_contents to write it back.