use fwrite instead of file_put_contents for lower memory consumption