请输入您要查询的百科知识:

 

词条 base64_encode
释义

函数说明

base64_encode

(PHP 4, PHP 5)

base64_encode — 使用 MIME base64 对数据进行编码

函数定义

string base64_encode ( string $data )

base64_encode() returns 使用 base64 对 data 进行编码。设计此种编码是为了使二进制数据可以通过非纯 8-bit 的传输层传输,例如电子邮件的主体。

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' . "\";

$headers .= 'Content-Type: multipart/mixed;

boundary="----=_NextPart_001_0011_1234ABCD.4321FDAC"' . "\";

$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");

?>

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/3/10 18:48:34