How to encode php file with base64

[PHP 4, PHP 5, PHP 7, PHP 8]

base64_encodeEncodes data with MIME base64

Description

base64_encode[string $string]: string

This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies.

Base64-encoded data takes about 33% more space than the original data.

Parameters

string

The data to encode.

Return Values

The encoded data, as a string.

Examples

Example #1 base64_encode[] example

The above example will output:

VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==

See Also

  • base64_decode[] - Decodes data encoded with MIME base64
  • chunk_split[] - Split a string into smaller chunks
  • convert_uuencode[] - Uuencode a string
  • » RFC 2045 section 6.8

gutzmer at usa dot net

11 years ago

For anyone interested in the 'base64url' variant encoding, you can use this pair of functions:

biziclop at vipmail dot hu

4 years ago

gutzmer at usa dot net's [ //php.net/manual/en/function.base64-encode.php#103849 ] base64url_decode[] function doesn't pad longer strings with '='s. Here is a corrected version:

Rasmus Schultz

4 years ago

In PHP 7, the padding issue with base64_decode[] is no more - the following is totally fine:

function base64_encode_url[$string] {
    return str_replace[['+','/','='], ['-','_',''], base64_encode[$string]];
}

function base64_decode_url[$string] {
    return base64_decode[str_replace[['-','_'], ['+','/'], $string]];
}

Checked here with random_bytes[] and random lengths:

//3v4l.org/aEs4o

MitMacher

13 years ago

Unfortunately my "function" for encoding base64 on-the-fly from 2007 [which has been removed from the manual in favor of this post] had 2 errors!
The first led to an endless loop because of a missing "$feof"-check, the second caused the rare mentioned errors when encoding failed for some reason in larger files, especially when
setting fgets[$fh, 2] for example. But lower values then 1024 are bad overall because they slow down the whole process, so 4096 will be fine for all purposes, I guess.
The error was caused by the use of "empty[]".

Here comes the corrected version which I have tested for all kind of files and length [up to 4,5 Gb!] without any error:

Hayley Watson

9 years ago

Base64 encoding of large files.

Base64 encoding converts triples of eight-bit symbols into quadruples of six-bit symbols. Reading the input file in chunks that are a multiple of three bytes in length results in a chunk that can be encoded independently of the rest of the input file. MIME additionally enforces a line length of 76 characters plus the CRLF. 76 characters is enough for 19 quadruples of six-bit symbols thus representing 19 triples of eight-bit symbols. Reading 57 eight-bit symbols provides exactly enough data for a complete MIME-formatted line. Finally, PHP's default buffer size is 8192 bytes - enough for 143 MIME lines' worth of input.

So if you read from the input file in chunks of 8151 [=57*143] bytes you will get [up to] 8151 eight-bit symbols, which encode as exactly 10868 six-bit symbols, which then wrap to exactly 143 MIME-formatted lines. There is no need to retain left-over symbols [either six- or eight-bit] from one chunk to the next. Just read a chunk, encode it, write it out, and go on to the next chunk. Obviously the last chunk will probably be shorter, but encoding it is still independent of the rest.



Conversely, each 76-character MIME-formatted line [not counting the trailing CRLF] contains exactly enough data for 57 bytes of output without needing to retain leftover bits that need prepending to the next line. What that means is that each line can be decoded independently of the others, and the decoded chunks can then be concatenated together or written out sequentially. However, this does make the assumption that the encoded data really is MIME-formatted; without that assurance it is necessary to accept that the base64 data won't be so conveniently arranged.

luke at lukeoliff.com

11 years ago

A function I'm using to return local images as base64 encrypted code, i.e. embedding the image source into the html request.

This will greatly reduce your page load time as the browser will only need to send one server request for the entire page, rather than multiple requests for the HTML and the images. Requests need to be uploaded and 99% of the world are limited on their upload speed to the server.



used as so


.logo {
    background: url[""] no-repeat right 5px;
}

or

Chủ Đề