Try using the following, with the addition of + at the end of w (i.e. w+).
'w+' - Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
$fp = fopen("com8", "w+");