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!
I want to read from and write to the file but for some reason I can't read from it with either fread() or fgets(). If I use 'r' or 'r+' I can read from it OK but of course when I write to it the old contents has not been deleted.
Hmmm that sounds incredibly strange. My first though was that read permissions (or execute permissions for the leading path) were not on for PHP but since fopen($bob, 'r'); works that cant be it
Any errors?
EDIT | Ooops, yet another misunderstood question today Someone find a nice lady to give me a hug
Last edited by Chris Corbyn on Tue Jul 19, 2005 2:31 pm, edited 1 time in total.
You can't. w+ and w both truncate (clear) the file upon opening it. Once you write information to it, however, you can read THAT back. I would open the file for reading, read any info you need, then close it and open it for writing.
Or, the better, faster, easier way, use file_get_contents to read all the info before you open it for writing.
Here's the problem! I want to lock the file... then read from it... then write to it... and then finally unlock it and release it. If I do a file_get_contents how can I be sure nothing has written to the file between when I read it and when I write to it?
What do you mean "lock" it? If you call file_get_contents, that reads all the information from it in one line. There should be less than 5 milliseconds between it. If you mean you want to CHMOD it, then allow owner read/write privelege, but lock everyone else's (0700), then afterwards, CHMOD it back to 0777.
What I am saying is I want to lock it (flock) before reading from it so I can be certain that nothing gets written to it between the read and the write. Even if only 5 milliseconds elapses between read and write this is long enough to allow another aplication to access or write to the file.
Not that simple. I want to open the file... create it if it doesn't exist. I need 'w' or 'w+' for this which deletes the content. Once opened I want to lock it (flock)... Then read from it which I can't do if I deleted the contents with 'w+'. Then I want the file emptied... then I want to write to it.
If I use file_get_contents I can't look the file before using it. If I use 'W+' it empties the file before I can read from it. Catch 22
$tmp = array(); //Just for temp storage
$fp = fopen($file, 'r+'); //Open for read/write fp at beginning
flock($fp, LOCK_EX); //Lock it
while (!feof($fp)) { //Not at end of file
$tmp[] = fgets($fp); //Next line onto stack
}
$tmp[] = $tmp[count($tmp)-1]; //Duplicate last line
$data = implode('', $tmp); //Into a string
fwrite($fp, $data); //Write back to file
flock($fp, LOCK_UN); //Unlock it
fclose($fp);
If you try running your script you will find that it doesn't just read the data and then write it back to the file. Every time you run it the file doubles in size. It is duplicating the data.
Edit: Actually it triplicates the data.
Last edited by bokehman on Tue Jul 19, 2005 3:46 pm, edited 1 time in total.
bokehman wrote:If you try running your script you will find that it doesn't just read the data and then write it back to the file. Every time you run it the file doubles in size. It is duplicating the data.
omg... I didn't expect it to work perfectly it was supposed to be pseudo but either way flock() is what you need in combination with fopen($fp, 'a+'); and fseek($fp, 0);