base64_decode

(PHP 3, PHP 4, PHP 5)

base64_decode -- 对使用 MIME base64 编码的数据进行解码

描述

string base64_decode ( string encoded_data )

base64_decode()encoded_data 进行解码,返回原始数据,失败则返回 FALSE。返回的数据可能是二进制的。

例子 1. base64_decode() 示例

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

此示例将显示:

This is an encoded string

参见 base64_encode()RFC 2045 6.8 章节。


add a note add a note User Contributed Notes
23-Oct-2006 07:57
BEWARE!!!:  base64_decode DOES NOT return FALSE on failure and return garbled text instead. Even, when the string was not abse64 encoded!!!
Starson
19-Sep-2006 09:23
To expand on Jes' post:

The change took place between 5.0.5 and 5.1.0.  Exactly where I don't know or care.

In short php <= 5.0.5's base64_decode( $string ) will assume that a space is meant to be a + sign where php >= 5.1.0's base64_decode( $string ) will no longer make that assumption.  I did not see this noted in the change log.

Please note that, as of this writing, mb_convert_encoding( $string, "UTF-8", "BASE64" ) still behaves as base64_decode( $string ) did in php <= 5.0.5 regardless of the version of php you are running.
Jes
30-Aug-2006 11:31
I've come across an interesting issue with an external program that submits a gzcompressed base64_encoded string to PHP via POST. The external program encodes the string using the occasional " " (space) character, however if I encode the same original string within PHP (using base64_encode), it uses a "+" (plus) character wherever the external program would use a space. On my deployed machine, running PHP 4.3.9, base64_decode is fine with the " " (space) characters, but my test server running 5.1.4 is not. It took me a while to figure out that was the issue, but I ended up fixing it with a simple:

<?php

$post_data
= str_replace(" ","+",$_POST['string'])

?>

This fix works on both the 4.3.9 and 5.1.4 machines. I am sure that the external program is probably not conforming to the standard, and it isn't a PHP problem per-se; but incase anybody else ever runs into that.
gabor dot barta at freemail dot hu
08-Jan-2005 10:09
Hi,

I just would like to add a comment, i was strugging with it for some time. So i opened a POP3 mailbox fopen, and listed the emails with attachments. I copied the base64 encoded text to a file and saved it. Then i tried to decode this file to a jpeg picture, and it just didnt work... Then i realized that there were some spaces in the text file....

Hope it will help anyone.

Bobi
paul at ijsfontein dot nl
10-Jun-2004 09:04
The user notes posted here helped me a lot in writing the PHP code to upload uuencoded files to a server using $_POST. Hardest thing to figure out was why the files came out scrambled and corrupted. After comparing the original file with the file reconstructed by the uudecode script, I found out that a simple "stripcslashes" on the posted data will do the trick.

So, to upload any kind of uuencoded file using a POST:
1. send the raw file data to the PHP script
2. $uuencoded_data = stripcslashes($_POST['filedata']);
3. strip the marker lines from $uuencoded_data (first line, last line and second last line of the data. Each line is seperated by a LF (chr(10)) character.)
4. $decoded_data = uudecode($stripped_uuencoded_data); (this function can be found in the user notes here).
5. Use the script provided in one of the user notes on this page to write the decoded data to a binary file.

That should do the trick!
tobias at silverxnet dot de
24-Dec-2003 04:16
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.
dharwood at synopsistechnologies dot com
06-Dec-2003 03:34
Encode/DEcode PAGE.

Here is a page that will accept plain text password and return an encoded password.  It will also do the opposite.  It will allow you to enter an encoded password and generate the plain text password... (just in case you forgot your password.)

<?php

if (isset($_POST['text_pass'])){
  
$but=$_POST['button'];
  
process_form();
}else{
  
print_form();
}

function
process_form(){

  switch (
$_POST['button']){
   case
"Get Encoded":

      
$text_pass = $_REQUEST['text_pass'];
       echo
"<html>\n";
       echo
"<head>\n";
       echo
"<title>Password Encoding</title>\n";
       echo
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
       echo
"</head>\n";
       echo
"<body>\n";
       echo
"<table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">\n";
       echo
"<div align=\"center\">\n";
       echo
"<h3 align=center>Password Encoding Page</h3>\n";
       echo
"<table align=\"center\" border=\"0\">\n";
       echo
"<tr>\n";
       echo
"<td ><b>You Entered:  </b></td>\n";
       echo
"<td>$text_pass</td>\n";
       echo
"</tr>\n";
      
$test = base64_encode(serialize($text_pass));
       echo
"<tr>\n";
       echo
"<td ><b>Your encoded password (base 64) is:  </b></td>\n";
       echo
"<td>$test</td>\n";
       echo
"</tr>\n";
      
$untest = unserialize(base64_decode($test));
       echo
"<tr>\n";
       echo
"<td ><b>To double test, your UNencoded password (base 64) is:  </b></td>\n";
       echo
"<td>$untest</td>\n";
       echo
"</tr>\n";
       echo
"</table>\n";
       break;
   case
"Get UNEncoded":
      
$text_pass = $_REQUEST['text_pass'];
       echo
"<html>\n";
       echo
"<head>\n";
       echo
"<title>Password Encoding</title>\n";
       echo
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
       echo
"</head>\n";
       echo
"<body>\n";
       echo
"<table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">\n";
       echo
"<div align=\"center\">\n";
       echo
"<h3 align=center>Password Encoding Page</h3>\n";
  
       echo
"<table align=\"center\" border=\"0\">\n";
       echo
"<tr>\n";
       echo
"<td ><b>You Entered:  </b></td>\n";
       echo
"<td>$text_pass</td>\n";
       echo
"</tr>\n";
      
$untest = unserialize(base64_decode($text_pass));
       echo
"<tr>\n";
       echo
"<td ><b>Your UNencoded password (base 64) is:  </b></td>\n";
       echo
"<td>$untest</td>\n";
       echo
"</tr>\n";
       echo
"</table>\n";
  
   default:
  
       break;
  }
}

function
print_form(){
   echo
"<html>\n";
   echo
"<head>\n";
   echo
"<title>Password Encoding</title>\n";
   echo
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
   echo
"</head>\n";
   echo
"<body>\n";
   echo
"<table border=\"0\" cellspacing=\"0\" width=\"100%\">\n";
   echo
"<div align=\"center\">\n";
   echo
"<h3 align=center>Password Encoding Page</h3>\n";
   echo
"<tr>\n";
   echo
"<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">\n";
   echo
"<table align=\"center\" border=\"0\">\n";
   echo
"<tr>\n";
   echo
"<td align=\"right\"><b>Password to Encode/Decode:</b></td>\n";
   echo
"<td align=\"right\">";
   echo
"  <input type=\"text\" name=\"text_pass\" size=\"30\" >\n";
   echo
"</td>\n";
   echo
"</tr>\n";
   echo
"</tr>\n";
   echo
"<tr>\n";
   echo
"<tr>\n";
   echo
"&nbsp;\n";
   echo
"</tr>\n";
   echo
"<td >";
   echo
"<input type=\"submit\" name=\"button\" value=\"Get Encoded\"><br>\n";
   echo
"</td>";
   echo
"<td >";
   echo
"<input type=\"submit\" name=\"button\" value=\"Get UNEncoded\"><br>\n";
   echo
"</td>";
   echo
"</tr>\n";
   echo
"</table>\n";
   echo
"</form>\n";
   echo
"<tr>\n";
   echo
"</table>\n";
   echo
"</body>\n";
   echo
"</html>\n";
}
?>
dhirendrak at yahoo dot com
07-Aug-2003 04:19
Heading : Your customizable encode
and decode function.

A customizable encoded and decode
Function which can encode and decode
the data. You need to pass variable to
the encode function to encrypt and
whenever you want you can get the
data by using decode function.

Cracking / hacking of encoded value
is impossible, until user know the
actual coding.

<?php
function encoded($ses)
{   
 
$sesencoded = $ses;
 
$num = mt_rand(3,9);
  for(
$i=1;$i<=$num;$i++)
  {
    
$sesencoded =
    
base64_encode($sesencoded);
  }
 
$alpha_array =
  array(
'Y','D','U','R','P',
 
'S','B','M','A','T','H');
 
$sesencoded =
 
$sesencoded."+".$alpha_array[$num];
 
$sesencoded =
 
base64_encode($sesencoded);
  return
$sesencoded;
}
//end of encoded function

function decoded($str)
{
  
$alpha_array =
   array(
'Y','D','U','R','P',
  
'S','B','M','A','T','H');
  
$decoded =
  
base64_decode($str);
   list(
$decoded,$letter) =
  
split("\+",$decoded);
   for(
$i=0;$i<count($alpha_array);$i++)
   {
   if(
$alpha_array[$i] == $letter)
   break;
   }
   for(
$j=1;$j<=$i;$j++)
   {
    
$decoded =
      
base64_decode($decoded);
   }
   return
$decoded;
}
//end of decoded function
?>

http://members.lycos.co.uk/dhirendrak
Klaus Fehrenbacher
17-Apr-2003 07:05
this script can correct the bug

<?php
$enc
= chunk_split(preg_replace('!\015\012|\015|\012!','',$enc));
$enc = base64_decode($enc);
?>
nsayer at kfu dot com
22-Mar-2002 05:15
I used to do uudecode as a C module, but I've discovered a really fast way to do it in PHP. Here it is:

<?php
function uudecode($encode) {
 
$b64chars="ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz0123456789+/"
;

 
$encode = preg_replace("/^./m","",$encode);
 
$encode = preg_replace("/\n/m","",$encode);
  for(
$i=0; $i<strlen($encode); $i++) {
   if (
$encode[$i] == '`')
    
$encode[$i] = ' ';
  
$encode[$i] = $b64chars[ord($encode[$i])-32];
  }

  while(
strlen($encode) % 4)
  
$encode .= "=";

  return
base64_decode($encode);
}
?>

This is the PHP equivalent to perl's unpack("u",___). That is, you need to strip the 'begin' and 'end' lines from the typical uuencoded file.