Help Writing to Flat Files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Help Writing to Flat Files

Post by Jim »

I'm creating something that requires I write to .txt files without overwriting previous information.

The written-in information will be HTML, and should be added above previous information. Any idea how to do that? Thanks!
User avatar
roninblade
Forum Newbie
Posts: 21
Joined: Thu Jun 13, 2002 7:12 pm

Post by roninblade »

just open open the file with "a" or "a+" then save your info to the file. or if you want to open it using "r+" you can use fseek() to move the file pointer to a position inside the file or to the end of the file.[/b]
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

Awesome. Is there a way to write to a specific part of the .txt file only?

For instance:

I have three articles, each with three different tables per entry: one with a picture, one with a name, one with a description. How would I edit ONLY the description of a specific name/pic/description or a specific entry?

Here's an example URL:

http://www.aoe2.com/!scripts/filesubmission/

See how I have three different fields? The form up top, the image box on the left and the description on the right?

When I select a map, I want it's description to come up, and I want to be able to add/edit a picture of the map. Then, when I edit any of those fields, I want the edit to go to a .txt file that contains ALL the other maps on it.

How?
User avatar
roninblade
Forum Newbie
Posts: 21
Joined: Thu Jun 13, 2002 7:12 pm

Post by roninblade »

how are your text files formatted? i mean, how are the information arranged inside the file?
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

I plan on putting everything in tables, but the title is going to be in the dropdown box.

<table>
<tr>
<td>Picture</td>
<td>Description</td>
</tr>
</table>

<!-- Next Item -->
<table>
<tr>
<td>Picture</td>
<td>Description</td>
</tr>
</table>

Thanks!
User avatar
roninblade
Forum Newbie
Posts: 21
Joined: Thu Jun 13, 2002 7:12 pm

Post by roninblade »

uhmmm... maybe you could format your flat file this way :

Picture#Description
Picture#Description
Picture#Description

i'm assuming here that the picture has a unique value. when you want to edit save each line into an array then edit the value of the line you want to change.

a bit something like this

Code: Select all

<?php
  $ncontents = array();
  $file_name = "path/to/your/file.here";
  $contents = file($file_name); // save file into an array
  foreach($contents as $val) &#123;
    $new = explode("#", $val);
    if (in_array($your_test_value, $new)) &#123;
      // change the value of the new array
      $new&#1111;0] = "new picture value";
      $new&#1111;1] = "new description value";
    &#125;
    $ncontents&#1111;count($ncontents)] = implode("#", $new);
  &#125;

  // save the file
  $finalvalue = implode("\n", $ncontents);
  $fp = fopen($file_name, "w");
  $fresult = fwrite($fp, $finalvalue); 
  fclose($fp);
?>
i haven't tested this code, though. but, i hope this helps.
Post Reply