I thought the file was written as sitemap.bb.xml originally, which would not overwrite your sitemap.xml file, correct?

Also, that $sitemap_file is normally defined with server $_SERVER['DOCUMENT_ROOT'], so you should be using a filesystem path, not a URL as you have done. Something like /var/www/you/forum/sitemap.xml instead of the url with HTTP.