rewrote tempfile logic for php 5.3