Home > Backend Development > PHP Tutorial > PHP Base64 encoded file binary stream is mainly used_PHP tutorial

PHP Base64 encoded file binary stream is mainly used_PHP tutorial

WBOY
Release: 2016-07-13 10:50:10
Original
1438 people have browsed it

This article will introduce to you an article about where the binary stream of php Base64 encoded files is mainly used. Friends who are interested can refer to it. I will only briefly describe it.

Base64 encoded file binary stream uses the base64_encode function to encode file binary information.

Official description

base64_encode — Encode data using MIME base64

Report a bug Description

string base64_encode ( string $data )
Encode data using base64.

This encoding is designed to enable binary data to be transmitted over non-pure 8-bit transport layers, such as the body of an email.

Base64-encoded data takes up about 33% more space than the original data.

The specific method is:

The main purposes of the above processing process are:
The code is as follows
 代码如下 复制代码

$path = 'image.jpg';
$fp = fopen($path, 'rb');  // 以二进制形式打开文件
$content = fread($fp, filesize($path)); // 读取文件内容
fclose($fp);
$content = base64_encode($content); // 将二进制信息编码成字符串

// echo $content;

上述程序输出的结果类似:R0lGODlhEAAQAJECAISEhAAAhP///wAAACH5BAEAAAIALAAAAAAQABAAAAImlI
+pyxedQADQhVflpfAK30jG1lwmqIgWl6CClmKHxn6mdVb6zhcAOw==

Copy code

$path = 'image.jpg';

$fp = fopen($path, 'rb'); // Open the file in binary form

$content = fread($fp, filesize($path)); // Read file content

fclose($fp);

$content = base64_encode($content); // Encode binary information into a string

// echo $content;

The output of the above program is similar to: R0lGODlhEAAQAJECAISEhAAAhP///wAAACH5BAEAAAIALAAAAAAQABAAAAImlI

+pyxedQADQhVflpfAK30jG1lwmqIgWl6CClmKHxn6mdVb6zhcAOw==

In this way we successfully converted a file into a string.

The decoding process is very simple, just use base64_decode($content).

1. Interface transmission

Mainly suitable for transferring files from one site to another through the WEB interface, and can be used for XML information.

Of course, you don’t need to do this to save file information such as pictures into the database, but this method is still applicable. This approach is more acceptable for database newbies. Because this is exactly a string. 3. File encryption File encryption is rarely used by everyone. For example, if we have a set of PHP programs that need to be protected and require users with authorization codes to run normally, then we may use the authorization codes to encrypt the files, which is the above encoding The resulting string is processed again. The running process requires an authorization code to run. Of course, there are other uses, which can be used flexibly according to individual needs.
http://www.bkjia.com/PHPjc/632665.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632665.htmlTechArticleThis article will introduce to you an article about where the binary stream of php Base64 encoded files is mainly used. There are some examples. Friends can refer to it, I just briefly described it. Base64 encoded file...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template