Is it possible to run multiple instances of a PHP script in parallel and have them write results to the same file without data corruption?

When running multiple instances of a PHP script in parallel that write results to the same file, data corruption can occur due to simultaneous write operations. To prevent this, you can use file locking mechanisms to ensure that only one instance writes to the file at a time. This can be achieved using the `flock()` function in PHP to acquire an exclusive lock on the file before writing to it.

$fp = fopen('results.txt', 'a');
if (flock($fp, LOCK_EX)) {
    fwrite($fp, "Result data\n");
    flock($fp, LOCK_UN);
} else {
    echo "Could not lock file!";
}
fclose($fp);