PHP 8.4.0 RC4 available for testing

base64_decode

(PHP 4, PHP 5, PHP 7, PHP 8)

base64_decodeDecodifica dados no formato MIME base64

Descrição

base64_decode(string $string, bool $strict = false): string|false

Decodifica uma string codificada em base64.

Parâmetros

string

Os dados codificados.

strict

Se o parâmetro strict for definido como true, então a função base64_decode() retornará false se a entrada contiver caracteres fora do alfabeto base64. Caso contrário, os caracteres inválidos serão descartados silenciosamente.

Valor Retornado

Retorna os dados decodificados ou false em caso de falha. Os dados retornados podem ser binários.

Exemplos

Exemplo #1 Exemplo base64_decode()

<?php
$str
= 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo
base64_decode($str);
?>

O exemplo acima produzirá:

This is an encoded string

Veja Também

adicione uma nota

Notas Enviadas por Usuários (em inglês) 7 notes

up
75
winkelnkemper at googlemail dot com
13 years ago
If you want to save data that is derived from a Javascript canvas.toDataURL() function, you have to convert blanks into plusses. If you do not do that, the decoded data is corrupted:

<?php
$encodedData
= str_replace(' ','+',$encodedData);
$decocedData = base64_decode($encodedData);
?>
up
3
martinstaemmler at gmx dot net
15 years ago
I had some trouble trying to let base64_decode decode base64-strings longer than ~5k chars.

The base64-decoding function is a homomorphism between modulo 4 and modulo 3-length segmented strings. That motivates a divide and conquer approach: Split the encoded string into substrings counting modulo 4 chars, then decode each substring and concatenate all of them.

Then instead of

<?php $decoded = base64_decode($encoded); ?>

for big $encoded strings, it's saver to use

<?php
$decoded
= "";
for (
$i=0; $i < ceil(strlen($encoded)/256); $i++)
$decoded = $decoded . base64_decode(substr($encoded,$i*256,256));
?>

where 256 can be replaced by a sufficiently small modulo 4 natural.
up
3
Tom
17 years ago
This function supports "base64url" as described in Section 5 of RFC 4648, "Base 64 Encoding with URL and Filename Safe Alphabet"

<?php
function base64url_decode($base64url)
{
$base64 = strtr($base64url, '-_', '+/');
$plainText = base64_decode($base64);
return (
$plainText);
}
?>
up
0
pete panic
9 months ago
Note, that padding characters are not limited to "=". any character(s) at the end of the string that cannot be decoded will be interpreted as padding. if $strict is set to true, of course padding characters are limited to base64 characters.

examples:

<?php
// $strict = false;
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo
base64_decode($str); // This is an encoded string

$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw';
echo
base64_decode($str); // This is an encoded string

$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZwA';
echo
base64_decode($str); // This is an encoded string

$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZwA=';
echo
base64_decode($str); // This is an encoded string

// $strict = true;
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo
base64_decode($str, true); // This is an encoded string

$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw';
echo
base64_decode($str, true); // This is an encoded string

$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZwA';
echo
base64_decode($str, true); // This is an encoded string

$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZwA=';
echo
base64_decode($str, true); // This is an encoded string
?>
up
0
walf
9 years ago
Base64 for URL parameters/filenames, that adhere to RFC 4648.
Defaults to dropping the padding on encode since it's not required for decoding, and keeps the URL free of % encodings.

<?php
function base64url_encode($data, $pad = null) {
$data = str_replace(array('+', '/'), array('-', '_'), base64_encode($data));
if (!
$pad) {
$data = rtrim($data, '=');
}
return
$data;
}
function
base64url_decode($data) {
return
base64_decode(str_replace(array('-', '_'), array('+', '/'), $data));
}
up
-4
user at sfdsfd dot com
5 years ago
function is_base64($str){
if($str === base64_encode(base64_decode($str))){
return true;
}
return false;
}

---------------------------------------------------------------------------
---------------------------------------------------------------------------
---------------------------------------------------------------------------

$str = 'VGhpcyBpcyBiYXNlNjQgZW5jb2RlIHN0cmluZw==';

if(is_base64($str)){
print base64_decode($str);
}
up
-5
tobias at silverxnet dot de
20 years ago
I was wondering how to decode attached images within mails. Basically they are mostly JPEG files, so it was obviously to write a function that decodes JPEG images.
I guess the plainest way to do so was the following:

<?php
function base64_to_jpeg( $inputfile, $outputfile ) {
/* read data (binary) */
$ifp = fopen( $inputfile, "rb" );
$imageData = fread( $ifp, filesize( $inputfile ) );
fclose( $ifp );
/* encode & write data (binary) */
$ifp = fopen( $outputfile, "wb" );
fwrite( $ifp, base64_decode( $imageData ) );
fclose( $ifp );
/* return output filename */
return( $outputfile );
}
?>

This function decodes the given inputfile (a filename!) and saves it to the given outputfile (a filename as well) and then returns the output filename for further usage (e.g. redirect, imagejpeg() and so on).
I thought that might be helpful.
To Top