Page 1 of 1
Help Writing to Flat Files
Posted: Thu Jun 13, 2002 6:59 pm
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!
Posted: Thu Jun 13, 2002 7:12 pm
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]
Posted: Thu Jun 13, 2002 7:36 pm
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?
Posted: Thu Jun 13, 2002 11:20 pm
by roninblade
how are your text files formatted? i mean, how are the information arranged inside the file?
Posted: Thu Jun 13, 2002 11:26 pm
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!
Posted: Fri Jun 14, 2002 12:20 am
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) {
$new = explode("#", $val);
if (in_array($your_test_value, $new)) {
// change the value of the new array
$newї0] = "new picture value";
$newї1] = "new description value";
}
$ncontentsїcount($ncontents)] = implode("#", $new);
}
// 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.