I this piece of code:
$file = fopen($path, 'r+');
flock($file, LOCK_EX);
// reading the file into an array and doing some stuff to it
for ($i=0; $i<count($array); $i++)
{
fwrite($file, $array[$i]);
}
flock($file, LOCK_UN);
fclose($file);
Basically what I want to do is: open a file > lock it > read it > do some stuff > clear the file > write to file > unlock it > close it.
The problem is the clearing part. I know I could do it with fopen($file, 'w+') but then reading would be a problem. Maybe I can change the mode somehow?
Any help would be appreciated, Paul
If you set the pointer to 0 using
fseekyou can then runftruncatelike so:With
ftruncatebe aware of these issues regard the second param:If size is smaller than the file then the file is truncated to that size.
http://www.php.net/manual/en/function.ftruncate.php