Is it possible to encode line by line in Base64?

Asked

Viewed 98 times

2

I have a code that takes binary files and converts them into Base64, but there are very large files and this ends up using much of the machine’s memory (and even process). To read large files generally (without converting them) I do this:

$handle = fopen('ARQUIVO', 'rb');

while (false === feof($handle)) {
    $data = fgets($handle);
    ...
}

fclose($handle);

So I avoid exceeding memory and other such problems. But the problem is that with base64_encode i need complete data (a string entire).

How to encode large binary files without losing performance?

  • Yes. Behold: But its pieces have to be in multiple sizes of 3 in a matter of bytes.

1 answer

1


It is possible yes, the code below was taken from a commenting about the function base64_encode.

$fh = fopen('Input-File', 'rb'); 
//$fh2 = fopen('Output-File', 'wb'); 

$cache = ''; 
$eof = false; 

while (1) { 
    if (!$eof) { 
        if (!feof($fh)) { 
            $row = fgets($fh, 4096); 
        } else { 
            $row = ''; 
            $eof = true; 
        } 
    } 

    if ($cache !== '') 
        $row = $cache.$row; 
    elseif ($eof) 
        break; 

    $b64 = base64_encode($row); 
    $put = ''; 

    if (strlen($b64) < 76) { 
        if ($eof) { 
            $put = $b64."\n"; 
            $cache = ''; 
        } else { 
            $cache = $row; 
        } 

    } elseif (strlen($b64) > 76) { 
        do { 
            $put .= substr($b64, 0, 76)."\n"; 
            $b64 = substr($b64, 76); 
        } while (strlen($b64) > 76); 

        $cache = base64_decode($b64); 

    } else { 
        if (!$eof && $b64{75} == '=') { 
            $cache = $row; 
        } else { 
            $put = $b64."\n"; 
            $cache = ''; 
        } 
    } 

    if ($put !== '') { 
        echo $put; 
        //fputs($fh2, $put); 
        //fputs($fh2, base64_decode($put));
    } 
} 

//fclose($fh2); 
fclose($fh); 
  • 1

    Just one note: I changed the size from 4096 to 32 $row = fgets($fh, 32); and used memory_get_usage() and memory_get_peak_usage() with an image of 2.75Mb, I got the following results: Initial: memory 224488 and memory_peak 237032 / Final: memory 224792 memory_peak 241600

Browser other questions tagged

You are not signed in. Login or sign up in order to post.