How can a PHP script be designed to lock a file for other users when it is opened and unlock it after it has been saved, as discussed in the thread?
To lock a file for other users when it is opened and unlock it after it has been saved, you can use file locking mechanisms provided by PHP. By using `flock()` function with `LOCK_EX` flag to acquire an exclusive lock on the file when opening and `LOCK_UN` flag to release the lock after saving the file, you can prevent other users from accessing the file while it is being edited.
$filename = 'example.txt';
$handle = fopen($filename, 'r+');
if (flock($handle, LOCK_EX)) {
// File locked, perform operations on the file
fwrite($handle, 'New content');
flock($handle, LOCK_UN); // Release the lock
} else {
echo 'Could not lock the file.';
}
fclose($handle);