Writing to a file. (PHP)

Nasimov

Beta member
Messages
4
It's possible to ammend data within a file vs. just adding data to the beginning or the end of the file? Mean to change something in the "middle" of the file.

Thanks.
 
I am not a PHP expert but I am sure it can be done. You will have to use something like the $curline function in Perl to search for something in the file and edit it.
 
I'm fairly sure you could use a pointer, the file opperations in PHP seem to be very closly linked to those in C...

Failing that you could read the entire string to a variable then use the split() function to remove the dat ayou wanted to keep/put bits in the middle of.

for instance if you wanted to insert a new row into the top of a table in an HTML document, you could do it like this

-----------------------
<?php
//open file for reading
$file = "/path/to/file.txt";
$link = fopen($file, "r");
//read file to string
$contents = fread($link, filesize($file));
fclose($link);
//get size of string
for($line=0;$line<sizeof($contents);$line++)

//split the start of the file after the table tag
//the first half goes to a[0] the second half goes to a[1]
$a = split("<table>", $contents[$line]);
$b = "your_stuff_you_want_to_insert "

//not sure if this string is consturcted right
//if it were asp it be text = a[]0 & yourstring & a[1]
$text = "$a[0] $b $a[1]"

//open file destructive open
$file = "/path/to/file.txt";
$link = fopen($file, "w");
//write string to file
//not sure if this is correct
fwrite($link, $text));
fclose($link);
?>
-----------------------------------------------------

Again i'm sory I've not tested this code before posting it (got many many assignments due). You might have to look some ov the details about how to use functions over in the PHP manual.
But I hoped this suggestion was beter than complete silence!
 
Back
Top Bottom