Home  >  Article  >  Backend Development  >  php base64 to binary stream

php base64 to binary stream

王林
王林Original
2023-05-06 13:42:071295browse

In Web development, the back-end language PHP is one of the most common languages. The data format conversion involved is also very common, among which Base64 is a method often used to encode binary data. For file uploading, image processing and other scenarios in web development, we need to convert data in Base64 format into a binary stream. This article will introduce how to use PHP to convert data in Base64 format into a binary stream.

1. What is Base64 encoding?

Base64 is an encoding method that can encode binary data into ASCII characters. It is often used in scenarios such as email transmission, HTTP protocol transmission, and encrypted storage. The main principle of Base64 encoding is to convert three 8-bit bytes into four 6-bit codewords and fill them with the "=" character. Since some special characters are removed from the 64 codewords, it is called Base64 encoding.

2. How to encode/decode Base64?

In PHP processing, Base64 encoding/decoding can directly use built-in functions. For a string, you can use base64_encode() to encode it. For a Base64-encoded data, you can use base64_decode() to decode it.

For example, the following code snippet implements string encoding and decoding.

$str = "hello world";
echo base64_encode($str); // Output aGVsbG8gd29ybGQ=
echo base64_decode("aGVsbG8gd29ybGQ="); // Output hello world
?>

3. How to convert data in Base64 format into a binary stream?

In PHP, for data in Base64 format, we can use the base64_decode() function to decode and store the decoded data. Taking the storage of pictures as an example, we need to convert the data in Base64 format into a binary stream (ie, a binary file).

The following code snippet demonstrates how to store image data in Base64 format into a local file.

##$base64_image = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfQAAAH0CAYAAAFOiXE1AAAABmJLR0QAAAAAAAD5Q7t/AAAAi0lEQVR4nO3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPwGgAAQAA8MoMOQAAIABJREFU3rNl3X1sldW /s /s /s /s /s /s /s /s /s /s /s /s /s + WPsB41EvXr81q3UKqrq56r440WfI5J5 5fjGdeua556PLlW5P6fdXNL6C19ve6 /Kvpt6Xi5eJhZsaGTUUQEB/gVgcF g1ZePJZZy e9XJcV7LjEzt3nZ4s53deJpEyl eiJtghgRPV7JF9X TtTwwwuPtPwmBwOdA2nlm12mWLOxIfm2ouq 195LFy9Xp8Wf/uAHMvZtvfWXzc5 N5d8/j5GZHfmNFRMUFBQQR6dddXJ6/v/1OTqrqc5fea1Z5aPOf968dMGSzqeDps24NGzeU6X B9iggUFhYKCgoyMjAIKApVVFRgR8evRQHxvXE3N3r6qqqpyx8fHBYGCgoKyswMwrIk dNBqNRqNjo7EQIC grK29mSEhJoJmYmBAQEAu0DQwJDQwNDQ2t8d/ rae/1vhNlOJiYlNcN7GvjYaGh4c3qNSHR4fzxoLj4 Kh4cVlZWeYGFYC7QNKyQkFBQVcaMGMAnQNKbVTh8dL1ePV7Cw8Ofn5 xlW5fcvvykpKELZ pN5i5PT5W5duvU6ejtyy /OjU6NAhENvaWlpOzs7KyoqJiRsbGygW8qtg2MzPz3K5Xr9WmzatEsstISejXL169fmzJljDaWlpb8QvTKywPypWrj9XrVmJSUlISEgIAAAAABJRU5ErkJggg==";
$file _path = "test.png";
$binary_data = base64_decode (str_replace('data:image/png;base64,', '', $base64_image));
$file = fopen($file_path, "wb");
fwrite($file, $binary_data);
fclose($file);
?>
Parse the following code:

First defines a string $base64_image in Base64 format, and defines the file to be stored File name (test.png). Then, call the str_replace() function to replace the data:image/png;base64, string in the header of the Base64-formatted string with nothing and convert it into binary data.

Next, define the file name where the file will be stored, open the file for reading and writing, write the corresponding binary data, and finally close the file.

In this way, the process of converting the image data in Base64 format into a binary stream and storing it in a local file is realized.

4. Summary

This article mainly introduces how to convert Base64 format data into a binary stream in PHP. Base64 encoding has a wide range of application scenarios in web development. For some scenarios that require transmitting or storing binary data, understanding Base64 encoding is very important. I hope that readers will have a deeper understanding of Base64 encoding and a better understanding of its use in practical applications through studying this article.

The above is the detailed content of php base64 to binary stream. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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