How does the LOCK_EX flag work in conjunction with file_put_contents() in PHP?

The LOCK_EX flag in PHP's file_put_contents() function allows you to acquire an exclusive lock on the file before writing to it, preventing other processes from accessing or modifying the file at the same time. This can be useful in scenarios where data integrity is critical and you want to ensure that only one process can write to the file at a time.

$file = 'example.txt';
$data = 'Hello, world!';

file_put_contents($file, $data, LOCK_EX);