Fundamentals 11 min read

Why Base64 Encoding Matters: Principles, PHP Implementation, and Real‑World Uses

This article explains the fundamentals of Base64 and Base64‑URL encoding, shows PHP functions for encoding and decoding, illustrates the encoding process with examples, and explores practical applications such as data URLs, MIME email attachments, and common pitfalls to avoid.

21CTO
21CTO
21CTO
Why Base64 Encoding Matters: Principles, PHP Implementation, and Real‑World Uses

In 2015 we built a "Baidu Cloud Observation" app on the Qingyun platform, which uses an iframe that sends data encoded with Base64 URL. The platform provides the following PHP encoding/decoding functions:

function base64_URL_encode($data) { return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); } function base64_URL_decode($data) { return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT)); }

Base64 URL is a variant of standard Base64 that replaces + and / with - and _ and removes trailing =.

The article addresses two questions: why use Base64 encoding and how the algorithm works.

Key points:

Computers store and execute binary (01) sequences; meaning is given by decoders.

Many protocols (e.g., HTTP) require data to consist of simple ASCII characters.

In the Qingyun example, a POST form sends a payload (JSON) and a signature. Special characters like & and / must be encoded to avoid conflicts, so the data is Base64‑URL encoded before transmission.

Base64 Encoding Principles

Base64 uses 64 printable characters to represent arbitrary binary data. The character set includes A‑Z, a‑z, 0‑9, +, and /. Variants change the last two characters.

It converts binary to text by grouping bits into 6‑bit units and mapping each unit to a character, expanding the data size by a 4/3 ratio.

Example: encoding "Hello!" yields SGVsbG8h. The process is illustrated below:

If the input length is not a multiple of three, padding with zero bits is added, and the resulting = characters are optional in URL‑safe variants.

Decoding reverses the process, discarding any padding bits that do not represent original data.

Most programming languages provide built‑in Base64 libraries (PHP, Python, Go, etc.).

Base64 Encoding Applications

One common use is embedding images directly in HTML via Data URLs. Browsers support the data: scheme, allowing binary files to be Base64‑encoded and placed inline in CSS or <img> tags, e.g.:

<img alt="Embedded Image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />

Large images should not be embedded this way because they increase page size.

MIME (Multipurpose Internet Mail Extensions)

Email protocols (SMTP) originally handled only ASCII. MIME adds Base64 encoding for binary attachments. An example email with a text file attachment shows the Content-Transfer-Encoding: base64 header and the encoded payload.

Avoid Misuse

Base64 is not encryption or a checksum. It is reversible and offers no security. For encryption use algorithms like AES‑128‑CBC or RSA; for integrity use HMAC with SHA‑256. Password storage should use salted hashes or HMAC, not plain Base64.

Summary

Base64 balances character set size and encoded length, and its variants make it adaptable to many scenarios, from web assets to email attachments, but it should be chosen based on actual requirements.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

encodingPHPBase64mimeData URLs
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.