在撰写文件时,flock()函数是否会在这种情况下防止文件损坏(如果两个进程碰巧尝试同时将数据写入文件)?
// Read the file data
$file_data = "Stuff you want to add\n";
$file_data .= file_get_contents('foo.txt');
// Open the file for writing and get an exclusive lock
$fp = fopen('foo.txt', 'w');
flock($fp, LOCK_EX);
// Write the new data at the start of the file
file_put_contents('foo.txt', $file_data);
// Close and unlock the file
fclose($fp);