The binary stream of BASE64 encoded files uses the Base64_encode function to encode file binary information.
Official description
Base64_encode-uses MIME base64 to encode data
Statement a bug description
String Base64_encode (String $data)
The data is encoded using Base64.
This encoding is designed to enable binary data to be transmitted through a 8-bit transport layer, such as the body of an e-mail message.
base64-encoded data consumes about 33% more space than the original data.
The specific way is:
| The code is as follows |
Copy Code |
|
$path = ' image.jpg ';
$fp = fopen ($path, ' RB '); Open a file in binary form
$content = Fread ($fp, FileSize ($path)); Read File contents
Fclose ($FP);
$content = Base64_encode ($content); Encode binary information as a string
Echo $content;
The results of the above program output are similar: R0lgodlheaaqajecaisehaaahp///waaach5baeaaaialaaaaaaqabaaaaimli
+pyxedqadqhvflpfak30jg1lwmqigwl6cclmkhxn6mdvb6zhcaow== |
So we succeeded in converting a file into a string.
The decoding process is very simple, using Base64_decode ($content).
The main uses of the above process are:
1. Interface transmission
The main application is to transfer files from one site to another through a web interface, which can be used for XML information.
2. Deposit into the database
Of course, saving pictures and other file information to the database can be done without doing so, but it still works. This is a more acceptable approach to the database novice. Because it's all a string.
3, file encryption
File encryption can be used less, for example, if we have a set of PHP programs need to protect the user must have authorization code to operate properly, then we may use the authorization code to encrypt the file, the above encoded string processing. The runtime requires an authorization code to run the process.
There are other uses, of course, that are flexibly used according to each individual's needs.