[ACCEPTED]-How to base64-decode large files in PHP-base64

Accepted answer
Score: 23

Even though this has an accepted answer, I 8 have a different suggestion.

If you are pulling 7 the data from an API, you should not store 6 the entire payload in a variable. Using 5 curl or other HTTP fetchers you can automatically 4 store your data in a file.

Assuming you are 3 fetching the data through a simple GET url:

$url = 'http://www.example.com/myfile.base64';
$target = 'localfile.data';

$rhandle = fopen($url,'r');
stream_filter_append($rhandle, 'convert.base64-decode');

$whandle = fopen($target,'w');

stream_copy_to_stream($rhandle,$whandle);
fclose($rhandle);
fclose($whandle);

Benefits:

  • Should be faster (less copying of huge variables)
  • Very little memory overhead

If 2 you must grab the data from a temporary 1 variable, I can suggest this approach:

$data = 'your base64 data';
$target = 'localfile.data';

$whandle = fopen($target,'w');
stream_filter_append($whandle, 'convert.base64-decode',STREAM_FILTER_WRITE);

fwrite($whandle,$data);

fclose($whandle);
Score: 11

Decode the data in smaller chunks. Four 4 characters of Base64 data equal three bytes 3 of “Base256” data.

So you could group each 2 1024 characters and decode them to 768 octets 1 of binary data:

$chunkSize = 1024;
$src = fopen('base64.data', 'rb');
$dst = fopen('binary.data', 'wb');
while (!feof($src)) {
    fwrite($dst, base64_decode(fread($src, $chunkSize)));
}
fclose($dst);
fclose($src);
Score: 1

It's not a good idea transfer 32Mb string. But 8 I have a solution for my task, that can 7 accept any size files from browser to app 6 server. Algorithm: Client

  1. Javascript: Read file from INPUT with FileReader and readAsDataURL() to FILE var.
  2. Cut all of data in FILE from start to first "," position Split it with array_chunks by max_upload_size/max_post_size php var.
  3. Send chunk with UID, chunk number and chunks quantity and wait for response, then send another chunk one by one.

Server Side Write each chunk until 5 the last one. Then do base64 with streams:

$src = fopen($source, 'r');
$trg = fopen($target, 'w');
stream_filter_append($src, 'convert.base64-decode');
stream_copy_to_stream($src, $trg);
fclose($src);
fclose($trg);

... now 4 you have your base64 decoded file in $target 3 local path. Note! You can not read and write 2 the same file, so $source and $target must 1 be different.

More Related questions