base64_encode

base64_encode() returns 使用 base64 對 data 進行編碼。設計此種編碼是為了使二進制數據可以通過非純 8-bit 的傳輸層傳輸,例如電子郵件的主體。

基本介紹

  • 中文名:base64_encode
  • 函式說明:base64_encode
  • 函式定義:Base64-encoded
  • 例子電子郵件的主體
函式說明,函式定義,實例說明,例-1,例-2,

函式說明

base64_encode
(PHP 4, PHP 5)
base64_encode — 使用 MIME base64 對數據進行編碼

函式定義

Base64-encoded 數據要比原始數據多占用 33% 左右的空間。

實例說明

例-1

使用base64_encode()函式對簡單字元串進行編碼。
<?php
$str = 'This is an encoded string';
echo base64_encode($str);
?>
此示例將顯示:
VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==

例-2

需要傳送一個多媒體MIME郵件,通過電子郵件附加檔案,而不是提供直接下載連結到一個檔案,這才是解決方案。
<?php
$tempfile = '/full/path/to/file.zip';
$thisfile = 'file.zip';
// Encode the file ready to send it off
$handle = fopen($tempfile,'rb');
$file_content = fread($handle,filesize($tempfile));
fclose($handle);
$encoded = chunk_split(base64_encode($file_content));
// create the email and send it off
$subject = "File you requested from RRWH";
$from = "這是一個電子郵件地址";
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-Type: multipart/mixed;
boundary="----=_NextPart_001_0011_1234ABCD.4321FDAC"' . "\n";
$message = '
This is a multi-part message in MIME format.
------=_NextPart_001_0011_1234ABCD.4321FDAC
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
Hello
We have attached for you the PHP script that you requested from RRWH as a zip file.
Regards
------=_NextPart_001_0011_1234ABCD.4321FDAC
Content-Type: application/octet-stream; name="';
$message .= "$thisfile";
$message .= '"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="';
$message .= "$thisfile";
$message .= '"
';
$message .= "$encoded";
$message .= '
------=_NextPart_001_0011_1234ABCD.4321FDAC--
';
// now send the email
mail($email, $subject, $message, $headers, "-f$from");
?>

相關詞條

熱門詞條

聯絡我們