PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




base64_encode

Name

base64_encode — Encodes data with base64 encoding.

Synopsis

string base64_encode(data);
string data: Data to be base64-encoded

Returns

Base64-encoded string; FALSE if the argument passed has no length when converted to a string

Description

base64_encode() encodes data using the base64 algorithm and returns the encoded data.

Note

Base64 encoding is used to encode data before it's transferred across legacy email systems that only support 7-bit ASCII. For more information on base64, refer to RFC 2045.

Version

PHP Version: 3+, 4+

See also

base64_decode()

Example

Example 1366. Use base64_encode() to create a simple MIME mail function

function simple_mime_mail ($to, $subject, $message) {
    // Find out how many arguments were passed to the function
    $num_args = func_num_args ();

    // Create a boundary string - boundary strings let the mail client
    // determine where the messages parts start and end.
    $mime_boundary = md5 (microtime() . $to . $subject);

    // Create the MIME headers used to separate the parts of the MIME message
    $mime_header   = "--$mime_boundary\r\n";
    $mime_header  .= "Content-type:text/plain\r\n";
    $mime_header  .= "Content-Transfer-Encoding:base64\r\n\r\n";
    
    // Start assembling the content of the MIME message
    // ...beginning with the overall header for the message
    $mime_content  = "Content-type:multipart/mixed; boundary=$mime_boundary\r\n\r\n";
    $mime_content .= "This is a MIME encoded message.\r\n\r\n";
    
    // Base64-encode the message
    // Use chunk_split() to wrap the lines to a reasonable length
    $message       = chunk_split (base64_encode ($message));
    
    // Add a set of MIME headers and the encoded message to the MIME message
    $mime_content .= "$mime_header$message\r\n";
    
    // Loop through any extra arguments to the function
    // Extra arguments should contain the names of files to attach to the message
    // Start grabbing arguments after the $to, $subject, and $message arguments.
    for ($offset = 3; $offset < $num_args; ++$offset) {
    
        // Retrieve a filename from the argument list
        $filename = func_get_arg ($offset);
        
        // Make sure that the argument is a valid filename
        if (! is_file ($filename)) {
            trigger_error ("File <tt>$filename</tt> is not a valid filename or is inaccessible");
            continue;
        }

        // Dump the contents of the file into an array
        // Join the array into one piece of data
        // Encode the data
        // Wrap the lines to a reasonable length
        $attachment = chunk_split (base64_encode (implode ('', file ($filename))));

        $mime_content .= "$mime_header$attachment\r\n";
    }

    return mail ($to, $subject, '', $mime_content);
}

// WARNING: This script will mail the contents of the file containing this script to the email
// address specified.  Don't include anything in this file that you would not want the world to see.
simple_mime_mail ('yourself@somehost.tld', 'Test Script', 'Testing 1, 2, 3', $PATH_TRANSLATED);
?>



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.