<?php
$file = 'file.txt';
$fp = fopen($file, 'ab');

if(($fp) !== false) {
    if(flock($fp, LOCK_EX) === true) {
        $data = 'Ajax Response!';

        if(fwrite($fp, $data)) {
            echo 'Write Ok!';
        } else {
            echo 'Write Error!';
        }

        flock($fp, LOCK_UN);
    } else {
        echo 'Locking Error!';
    }

    fclose($fp);
}
?>