It's not just whitespace. You can't send any data at all to the client (i.e. anything outside
tags) before you call header()
.
What you need to do is move your PHP code to the top of the file, before any of the HTML code (although in this case it looks like you're not actually expecting any HTML output from this script anyway, so you can probably get rid of it entirely).