Help!
This is driving me nuts...
I am working on some code to manipulate a text file. I can add and read back rows with no problem.
However.. I need to be able to move to a certain point in the file and modify the byte/char at that position.
I've done this many times in C, VBS & VB...
... but I just cant get it to work in PHP.
The crux of the problem is that no matter how I try to set the file pointer position (ie with rewind or fseek), data is ALWAYS appended to the end of the file.
Am I doing something wrong or is this correct behaviour and I've misread the docs for fopen/fwrite/rewind/fseek etc,.
To demonstrate, here is a bit of code...
<?
// create a file in non-desructive read/write
// mode (in real life, the file will pre-exist
// and data must be preserved not truncated
$fp=fopen("file_test.txt","a+");
if($fp)
{
// write some test data
fwrite($fp,"test data");
// go back to the start
rewind($fp);
// now read the first 9 chars
// (should get "test data")
echo fread($fp,9) . "<br>";
// now go back to the start again
rewind($fp);
fseek($fp,0);
// try to overwrite the first "t" with a "p"
fwrite($fp,"p");
// now go back yet again
rewind($fp);
fseek($fp,0);
// read whats there
// (want : "pest data")
// (get : "test data" with a "p"
// in the file after the word "data")
echo fread($fp,9) . "<br>";
fclose($fp);
}
?>
Before anyone says it, I know a db would do
the trick very easily.. but the hosting service (without mysql even) was not my choice...
Any pointers, clues etc, gratefully received!
john