$fd = fopen($my_file, 'a'); if ( $fd != 0 && flock($fd, LOCK_EX) && ftruncate( $fd, 0) ){ fwrite( $fd, $to_write ); } else { print 'failed..'; } fclose($fd);