2

I need to base64 encode big file with PHP.
file() and file_get_contents() are not options since them loads whole file into memory.
I got idea to use this:

$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        echo $buffer;
    }
    fclose($handle);
}

SOURCE: Read and parse contents of very large file

This working well for reading, but is it possible to do it like this:
Read line -> base64 encode -> write back to file
And then repeat for each line in file.
Would be nice if it could do it directly, without need to write to temporary file.

Community
  • 1
  • 1
xZero
  • 645
  • 2
  • 9
  • 24

1 Answers1

3

Base64 encodes 3 bytes of raw data into 4 bytes on 7-bit safe text. If you feed it less than 3 bytes padding will occur, and you can't have that happen in the middle of the string. However, so long as you're dealing in multiples of 3 you're golden, sooo:

$base_unit = 4096;
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
    while (($buffer = fread($handle, $base_unit*3)) !== false) {
        echo base64_encode($buffer);
    }
    fclose($handle);
}

http://en.wikipedia.org/wiki/Base64#Examples

Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • It doesn't helps much since it doesn't write back to file in same time. read line -> base64 encode line -> write line back – xZero Mar 24 '15 at 18:10
  • You cannot re-write a file in-place, you have to write it out to a new file and then change the name at the end. – Sammitch Mar 24 '15 at 18:17
  • Okay, that helps. Can I write to another file line by line in same time? – xZero Mar 24 '15 at 18:19
  • Yes, just open another handle. – Sammitch Mar 24 '15 at 18:32
  • 2
    Just a note--I would recommend using "fread()" in place of "fgets()", as the latter will stop at newline characters, causing unexpected behavior on many files. – TonyTheJet Jun 22 '18 at 17:12