If it runs great by it's self I would think the problem lies in how it's called via ajax.
I really don't see anything in this file that needs ajax anyway.
Why not just make it a function in the page?
If you insist on using ajax then post the code that calls the ajax and the ajax itself.
P.S. You just posted.
What does even unlink('pagename.php') have to do with anything?
Evolution - The non-random survival of random variants.