I see people using flock like this:
if (!$fp = fopen($file_name, 'wb'))
{
return FALSE;
}
if (flock($fp, LOCK_EX))
{
fwrite($fp, serialize($data));
flock($fp, LOCK_UN);
}
Also this:
if (!$fp = @fopen($file_name, 'rb'))
{
return FALSE;
}
flock($fp, LOCK_SH);
$data = '';
if (filesize($file_name) > 0)
{
$data = unserialize(fread($fp, filesize($file_name)));
}
But isn't there a chance that someone else will edit the file between the fopen
call and the flock
call? and the same question for fread
EDIT:
To clarify why I'm asking this... I'm basing my question on the code here, In a mysql caching situation, what's to stop 20 people from all being able to access the file at the same time if they all can get in between the fopen and flock?
Is that code foolproof?