i really cannot see how ob_start() would have anything to do with not being able to write to a file. are there any php errors you are getting?
also, you are overwriting $to_write at the end of your script, so its not allowing the js to be written to the file. are you doing that on purpose?