Home > Article > Backend Development > php base64 to binary stream
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==";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!