Sample code for 30+ languages & platforms
PHP

Convert any File to Base64 (and back)

Demonstrates how to get the contents of any file as a base64 string, and then write it back.

Chilkat PHP Downloads

Chilkat2 PHP
<?php

use Chilkat2\BinData;

$bd = new BinData();

// This example will load a PDF and return it as a base64 string.
try {
    $bd->LoadFile('qa_data/pdf/helloWorld.pdf');
} catch (\Chilkat2\Exception $e) {
    echo "Failed to load file.\n";
    exit;
}

$b64Str = $bd->GetEncoded('base64');
echo $b64Str, "\n";

// Now write the base64 string back to the binary PDF file:
$bd2 = new BinData();
$bd2->AppendEncoded($b64Str, 'base64');
$bd2->WriteFile('qa_output/helloWorld.pdf');