Your flock call is the check to see if it's already locked. If it's locked, that if() statement would fail, so you could just throw an else on it with something like:
if (flock($file, LOCK_EX)) {//lock was successful
fwrite($file,$write_contents);
} else {
echo "$file is locked.";
}