Home Backend Development PHP Problem How to convert string or binary data to Base64 encoding in php

How to convert string or binary data to Base64 encoding in php

Apr 12, 2023 am 09:17 AM

In modern Web development, there are many ways to transmit data, among which Base64 encoding has become a common encoding method. In PHP programming, encoding string or binary data into Base64 encoding is also very simple.

In this article, we will introduce how to convert string or binary data to Base64 encoding in PHP, and how to decode Base64 encoding. We will also discuss some practical uses of Base64 encoding.

1. What is Base64 encoding?

Base64 is a 64-character encoding method used to convert binary data into plain text format for easy use in network transmission or storage. The Base64 encoding table includes uppercase and lowercase letters, numbers, and some special characters, a total of 64 characters. As shown below:

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 /

In order to convert any binary data into readable ASCII code characters, Base64 encoding packs every three bytes into a group, and then these three Bytes are converted into a 64-character encoding format, ultimately generating a string composed of readable ASCII characters. If the number of bytes of data is not a multiple of 3, Base64 encoding adds some special characters at the end to make the entire encoding length a multiple of 4.

2. How to convert string and binary data into Base64 encoding in PHP?

Converting string and binary data to Base64 encoding in PHP is very simple. PHP provides a function base64_encode(), which can encode actual parameters into Base64 encoding.

The following is an example of encoding a string into Base64 encoding:

1

2

3

4

5

<?php

$str = "Hello World!";

$encoded_str = base64_encode($str);

echo $encoded_str;

?>

Copy after login

When the above code is executed, the output result is:

1

SGVsbG8gV29ybGQh

Copy after login

The above output result is Base64 encoding of the "Hello World!" string.

To encode binary data into Base64 encoding, just pass in the binary data as a parameter of the base64_encode() function.

The following is an example of encoding binary data into Base64 encoding:

1

2

3

4

5

<?php

$data = file_get_contents("image.jpg");

$encoded_data = base64_encode($data);

echo $encoded_data;

?>

Copy after login

When the above code is executed, the output result is Base64 encoding after encoding the binary data in the image.jpg file . In the above example, in order to read the data in the file into a string, we used the file_get_contents() function in PHP.

3. How to decode Base64 encoding in PHP?

Decoding Base64 encoding to raw data in PHP is also very simple. PHP provides a function base64_decode(), which can decode a Base64-encoded string into raw data.

The following is an example of decoding a Base64-encoded string into a string:

1

2

3

4

5

<?php

$encoded_str = "SGVsbG8gV29ybGQh";

$str = base64_decode($encoded_str);

echo $str;

?>

Copy after login

When the above code is executed, the output result is the string "Hello World!", which string That is the original data encoded by Base64 above.

For decoding Base64 encoding into binary data, the method is similar to the above example.

4. Some uses of Base64 encoding

In Web development, Base64 encoding has many applications. Here we briefly introduce its two common applications.

  1. Display of pictures or other binary data in Web pages

In Web development, pictures or other binary data need to be displayed to users through web pages. If binary data is used directly, many unpredictable problems will occur. For example, binary data may be truncated during transmission or cannot be parsed correctly. Base64 encoding can convert binary data into text format for easy display on the Web.

The following is an example of converting an image into Base64 encoding.

1

2

3

4

5

<?php

$data = file_get_contents("image.jpg");

$encoded_data = base64_encode($data);

echo "<img src=&#39;data:image/jpeg;base64," . $encoded_data . "&#39;/>";

?>

Copy after login

When the above code is executed, the output will display an img tag containing image.jpg image data.

  1. Application in API transmission

In modern applications, API interfaces are used for data transmission between different platforms. In API transmission, the size and type of data need to take into account the security and efficiency of data transmission. Base64 encoding can convert binary data into text format for easy use in API transmission.

Summary

In PHP development, converting string and binary data into Base64 encoding is very simple. PHP provides base64_encode() and base64_decode() functions to perform this operation. Base64 encoding has many applications in web development, such as displaying images or other binary data in web pages, and in API transmission for securely transmitting data.

The above is the detailed content of How to convert string or binary data to Base64 encoding in php. For more information, please follow other related articles on the PHP Chinese website!

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. Mar 25, 2025 am 10:37 AM

PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

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.

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 Encryption: Symmetric vs. asymmetric encryption. PHP Encryption: Symmetric vs. asymmetric encryption. Mar 25, 2025 pm 03:12 PM

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

PHP Authentication & Authorization: Secure implementation. PHP Authentication & Authorization: Secure implementation. Mar 25, 2025 pm 03:06 PM

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

How do you retrieve data from a database using PHP? How do you retrieve data from a database using PHP? Mar 20, 2025 pm 04:57 PM

Article discusses retrieving data from databases using PHP, covering steps, security measures, optimization techniques, and common errors with solutions.Character count: 159

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 CSRF Protection: How to prevent CSRF attacks. PHP CSRF Protection: How to prevent CSRF attacks. Mar 25, 2025 pm 03:05 PM

The article discusses strategies to prevent CSRF attacks in PHP, including using CSRF tokens, Same-Site cookies, and proper session management.

See all articles