Jump to content


Photo

placing something in the middle of a file.


  • Please log in to reply
8 replies to this topic

#1 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 October 2006 - 04:16 PM

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.

<!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>
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?

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 04 October 2006 - 04:29 PM

You could use fread fputs. Here is something that I use for updating named.conf

<?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);
?>

Hope that helps,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 October 2006 - 05:22 PM

This partially makes since, but this is my first time doing this, can you explain the process a little.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#4 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 04 October 2006 - 05:35 PM

Ok..

This part is pretty much self explanatory.

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

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

while (!feof($fp)){ 
    $line = trim(fgets($fp, 1024));

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">

if ($line == '#BEGIN AUTO RECORDS'){


The rest is explained fairly well  in my example.

Good Luck,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#5 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 04 October 2006 - 05:36 PM

<!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>

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

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

PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#6 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 04 October 2006 - 05:46 PM

^ 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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#7 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 October 2006 - 05:52 PM

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?

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#8 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 04 October 2006 - 05:53 PM

It will place the content below the marker..

EDIT 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.

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#9 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 October 2006 - 05:54 PM

ah ok, thanks.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users