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.
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!