Home >Backend Development >PHP Problem >How to convert php base64 to binary stream

How to convert php base64 to binary stream

藏色散人
藏色散人Original
2021-09-26 10:40:453404browse

php Base64 to binary stream method: 1. Create a PHP sample file; 2. Convert the base64 string into a binary stream through the "function base64_to_blob($base64Str){...}" method.

How to convert php base64 to binary stream

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer.

php How to convert base64 to binary stream?

php base64 string to binary stream

The code is as follows:

function base64_to_blob($base64Str){
    if($index = strpos($base64Str,'base64,',0)){
        $blobStr = substr($base64Str,$index+7);
        $typestr = substr($base64Str,0,$index);
        preg_match("/^data:(.*);$/",$typestr,$arr);
        return ['blob'=>base64_decode($blobStr),'type'=>$arr[1]];
    }
    return false;
}
$data = base64_to_blob($base64Str);
header('Location: '.$data['type']);
echo $data['blob'];

strpos( ) function finds the first occurrence of a string within another string.

The substr() function returns a part of a string.

base64_decode — Decode data encoded using MIME base64.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert 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