another alternative would be to use a stream-filter for base64-encoding. this will help against memory limitations without the need to dump the file.. use something like the following with an in-memory stream:
`stream_filter_append($fh, 'convert.base64-encode');`