Jump to content

Archived

This topic is now archived and is closed to further replies.

Ninjakreborn

placing something in the middle of a file.

Recommended Posts

Ok
I have gotten everything together for this based off advice here and there.

There is one element, I don't know how to do, or don't understand.
i have a way to open the file, get the info, close the file.
I have a way to take that information that was removed from that file, then I open/create a new file, and put that content within that file, set permissions on it, so the person can edit it with admin later, then close the file.

I need to figure out how to place something within a file.  FOr instance.

I have a file, it contains this.

[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Template 1</title>
</head>

<body>
<div id="content">
</div>
</body>
</html>[/code]
currently that's all it contains.  Later I am going to have to actually put a atemplate on the page, so I will be doing that manually within the template page.  Now what I need to figure out how to do, is dynamically create cms information within the page.  I need to get the database call to display the information to show up there, I know how to extract the information from the file, and create another file and paste all that text in there.  I don't know how to "open up" the file and place contents in the file where I need to place it at.

Chances are if it has something to do with finding a specific kilobyte position to start putting the information in, then that won't work, because when I go back and add in the styling for the page, and keyword preperation adn everything, it will change.  I can't ahve that happening?

Share this post


Link to post
Share on other sites
You could use fread fputs. Here is something that I use for updating named.conf

[code]
<?php
$newdomain = mysql_real_escape_string(trim(strip_tags($_POST['domain'])));
$file = 'path/to/file';
$fp = fopen($file, 'r+'); 
$layout = " zone \"$newdomain\" IN {
    type master;
    file \"$newdoman.zone\";
};
";

while (!feof($fp)){
    $line = trim(fgets($fp, 1024));
    //the line that you want to start at
    if ($line == '#BEGIN AUTO RECORDS'){
        //Find where we are so we can come back.
        $fpos = ftell($fp);
        //First, read the rest of the file, so we don't overwrite it.
        $rest = fread($fp, filesize($file));

        //Go back
        fseek($fp, $fpos, SEEK_SET);
        //Write the new data after the marker.
        fwrite($fp, "\n\n");
        fwrite($fp, $layout);
        //Write back the rest of the data.
        fwrite($fp, $rest);
    }
}
fclose($fp);
?>[/code]

Hope that helps,
Tom

Share this post


Link to post
Share on other sites
This partially makes since, but this is my first time doing this, can you explain the process a little.

Share this post


Link to post
Share on other sites
Ok..

This part is pretty much self explanatory.

[code=php:0]
$file = 'path/to/file';
$fp = fopen($file, 'r+');
//the layout of my zone information 
$layout = " zone \"$newdomain\" IN {
    type master;
    file \"$newdoman.zone\";
};
"; [/code]

Now we search the file for a line..
This goes through a loop and reads the file line by line

[code=php:0]
while (!feof($fp)){
    $line = trim(fgets($fp, 1024));
[/code]

This is the marker were we will right the records after. So say you are wanting to update the content of a file. You could call the line <div id="content">

[code=php:0]
if ($line == '#BEGIN AUTO RECORDS'){
[/code]


The rest is explained fairly well  in my example.

Good Luck,
Tom

Share this post


Link to post
Share on other sites
[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Template 1</title>
</head>

<body>
<div id="content">
 <!-- dynamically place content here !-->
 {CONTENT}
</div>
</body>
</html>[/code]

Are you saying you want to replace dynamic content from the DB where I've placed the {CONTENT}?

[code]
<?php
 // Build the page content into a variable $content
 $html = file_get_contents($file);
 $html = str_replace("{CONTENT}", $content, $html);
 echo $html;
?>
[/code]

Share this post


Link to post
Share on other sites
^ that will work if you are only wanting to replace the content once. If you are designing a CMS you are going to run into issues ether way. I haven't quite figured out how to erase the data between two tags.

Say I delete a domain. I just re write the entire name.conf. I don't think that will suit your needs. I have so many things that I am working on and havn't had the time to perfect my code.

Just play with the code that I posted. You should figure it out before to long.

Good Luck,
Tom

Share this post


Link to post
Share on other sites
if ($line == "<div id=\"content\">"){
Won't that overwrite my <div id="content"> or will it start writing below that line, and knock everything below it down as it goes, or will it start overwriting things on that page, or just move them down some lines??

THen for the line <div id="content"> will it overwrite it, or start putting the content below it?

Share this post


Link to post
Share on other sites
It will place the content below the marker..

[b]EDIT[/b] And yes it will bump every thing down.

Are you designing a CMS or ?? If so, I would research other methods of displaying dynamic content. I like to use javascript(Ajax). It is alot cleaner.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.