private function FilePutContents($file, $str) {
    $fileObj = new SplFileObject($file, 'w');
    while (!$fileObj->flock(LOCK_EX)) {
      usleep(1);
    }
    $bytes = $fileObj->fwrite($str);
    $fileObj->flock(LOCK_UN);
    return $bytes;
  }