★ wanayoo — archive 1999 http://fr.php.net/manual/cs/function.base64-encode.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links 
search for in the  
<base64_decodeparse_url>
Last updated: Fri, 20 Dec 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese (Hong Kong Cantonese) | Dutch | Finnish | French | German | Hebrew | Hungarian | Italian | Japanese | Korean | Polish | Romanian | Russian | Slovak | Slovenian | Spanish | Swedish | Turkish

base64_encode

(PHP 3, PHP 4 )

base64_encode -- Zakódovat data pomocí MIME base64

Popis

string base64_encode ( string data)

base64_encode() vrátí data zakódovaný pomocí base64. Toto kódování je navrženo tak, aby umožnilo binárním datům přežít transport přenosovými vrstvami, které nejsou osmibitové, jako jsou například těla emailů.

Data kódovaná pomocí base64 zabírají o zhruba 33% prostoru více než původní data.

Viz také: base64_decode(), chunk_split(), RFC-2045 sekce 6.8.

User Contributed Notes
base64_encode
add a note about notes
bullshit at burk dot dhs dot org
15-May-2002 11:14

There seems to be a problem using the base64_encode() under win32 envariments. I have recived some mails about it. It works fine under linux. The base64_decode() seems to work fine under both os.

Best regards,
Rolf
http://php.holtsmark.no/base64img/

23-Jul-2002 07:58
testing it script.

<?php
$s = "this is test data";
$e = base64_encode($s);
$d = base64_decode($e);
echo "original data: $s<dd>\t" .
    "encoded data: $e<dd>\t" .
    "decoded data: $d<dd>\t";
?>

emaman at upandup dot net
17-Sep-2002 12:43

Here is a workaround for the base64_encode function under Win32.
1) Download the base64.exe program from http://www.fourmilab.ch/.
2) Write your own base64 encode function :

   function mybase64_encode($s) {
    $tmpfname = tempnam("", "tmp");
    $fp = fopen($tmpfname, "w+");
    if ( $fp ) {
        fwrite ($fp, $s);
        fclose($fp);
        exec("c:/bin/base64 -e " . $tmpfname . " " . $tmpfname . ".b64");
        $fp = fopen($tmpfname . ".b64", "r");
            $contents = fread($fp,filesize($tmpfname . ".b64"));
        fclose($fp);
        unlink($tmpfname);
        unlink($tmpfname . ".b64");
        return($contents);
        }
  }
With this function you don't need to call the chunk_split function.

guy at bhaktiandvedanta dot com
02-Oct-2002 12:00

You can use base64_encode to transfer image file into string text and then display them. I used this to store my images in a database and display them form there. First I open the files using fread, encoded the result, and stored that result in the database. Useful for creating random images.

image.php:

<?

header(" Content-Type: image/jpeg");
header(" Content-Disposition: inline");
$sql = "SELECT data FROM image where name='".$img."'";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$image = $row[0];
echo base64_decode($image);

?>

And in the html file you put:

<img src=/old?u=http%3A%2F%2Ffr.php.net%2Fmanual%2Fcs%2F%26quot%3Bimage.php%3Fimg%3Dtest3%26quot%3B&y=1999 border="0" alt="">

Guy Laor

webmaster at mainbotics dot com
18-Nov-2002 07:49

In response to guy at bhaktiandvedanta dot com:
You can simply save the images in a blob field (Binary Large OBject) in the data base just by using mysql_escape_string(), which escapes NULL, CR, LF, \x1A, ... and other special-meaning characters, so you can directly insert the image into the sql query without any problem and without any kind of encoding (just the slashes). That way, you'll save space at the data base and when you get the image, you can directly dump it to the browser without making any kind of decoding. The script will so become faster and more efficient, and MySQL is already aware about the field contains binary data, not text data, to threat it in a convenient way.

maarten dot malaise at pandora dot be
18-Dec-2002 07:27

In addition to what "guy at bhaktiandvedanta dot com" and "webmaster at mainbotics dot com" wrote above
Here's a full example of how to upload a file and add it to a MySQL database

create file "upload.htm" to select and upload a file

<html>
<body>
<FORM ENCTYPE="multipart/form-data" ACTION=/old?u=http%3A%2F%2Ffr.php.net%2Fmanual%2Fcs%2F%26quot%3Bupload.php%26quot%3B&y=1999 METHOD=POST>
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="65000">
Send this file: <INPUT NAME="userfile" TYPE="file">
<INPUT TYPE="submit" VALUE="Upload file">
</FORM>
</body>
</html>

create file "upload.php" to add the file to the database (assuming that you are adding the file to an existing record with a BLOB field named "picture"):

<?

if (is_uploaded_file($userfile)) {
   copy($userfile, $HTTP_POST_FILES['userfile']['name']);
} else {
   echo "error. File: '$userfile'.";
}

$filename=$HTTP_POST_FILES['userfile']['name'];

$fd = fopen ($filename, "r");
$contents = fread ($fd, filesize($filename));
fclose($fd);

$escaped_contents=mysql_escape_string($contents);

// Connecting, selecting database
$link = mysql_connect("dbhost", "dbuser", "dbpassword")
   or die("Could not connect");

mysql_select_db("databasename")
   or die("Could not select database");

// Performing SQL query; assuming that the "picture" is a BLOB field
$query = "UPDATE tablename SET picture='$escaped_contents' WHERE id=1";
$result = mysql_query($query)
   or die("Query failed");

// Closing connection
mysql_close($link);

?>

create "view.php" to view the image (assuming that you uploaded a GIF-formatted file):

<?

// Connecting, selecting database
$link = mysql_connect("dbhost", "dbuser", "dbpassword")
   or die("Could not connect");
#print "Connected successfully";
mysql_select_db("databasename")
   or die("Could not select database");

header(" Content-Type: image/gif");
header(" Content-Disposition: inline");

$sql = "SELECT picture FROM tablename WHERE id=1";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$image = $row[0];
echo $image;

?>

And finally: this is how you can use the image in a HTML result file

<img src=/old?u=http%3A%2F%2Ffr.php.net%2Fmanual%2Fcs%2F%26quot%3Bview.php%26quot%3B%26gt%3B%3Cbr&y=1999>
good luck,

Maarten Malaise

add a note about notes
<base64_decodeparse_url>
Last updated: Fri, 20 Dec 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Sun Dec 29 04:09:34 2002 CET