base64_encode() returns 使用 base64 對 data 進行編碼。設計此種編碼是為了使二進制數據可以通過非純 8-bit 的傳輸層傳輸,例如電子郵件的主體。
基本介紹
函式說明
函式定義
實例說明
例-1
<?php
$str = 'This is an encoded string';
echo base64_encode($str);
?>
例-2
<?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");
?>