I had some trouble trying to let base64_decode decode base64-strings longer than ~5k chars.
The base64-decoding function is a homomorphism between modulo 4 and modulo 3-length segmented strings. That motivates a divide and conquer approach: Split the encoded string into substrings counting modulo 4 chars, then decode each substring and concatenate all of them.
Then instead of
<?php $decoded = base64_decode($encoded); ?>
for big $encoded strings, it's saver to use
$decoded = "";
for ($i=0; $i < ceil(strlen($encoded)/256); $i++)
$decoded = $decoded . base64_decode(substr($encoded,$i*256,256));
where 256 can be replaced by a sufficiently small modulo 4 natural.