I need to lock the file, read the data, write to the file and then close it. The problem that I have is I’m trying to find the correct mode for fopen.
With ‘a+’ – always append data, with ‘w+’ truncated all data when open, with ‘x+’ – fails to lock the file.
This is my code:
$fh_task = fopen($task_file, 'w+');
flock($fh_task, LOCK_EX) or die('Cant lock '.$task_file);
$opt_line = '';
while(!feof($fh_task)){
$opt_line .= fread($fh_task, 4096);
}
$options = unserialize($opt_line);
$options['procceed']++;
rewind($fh_task);
fwrite($fh_task, serialize($options));
flock($fh_task, LOCK_UN);
fclose($fh_task);
You want
'r+'(orc+if you’re using a newer version of PHP).r+doesn’t truncate (neither doesc+), but still allows you to write.here’s an excerpt from when I last worked with these functions: