search
HomeBackend DevelopmentPHP Problemphp base64 to binary stream

php base64 to binary stream

May 06, 2023 pm 01:42 PM

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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)