使用 PHP 将 Base64 字符串作为二进制文件保存到磁盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/380922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:33:08  来源:igfitidea点击:

Saving a Base64 string to disk as a binary using PHP

phpbinarybase64

提问by Paul Dixon

As a "look under the covers" tutorial for myself I am building a PHP script to gather emails from a POP3 mailbox. While attempting to make use of binary attachments I am stuck trying to figure out what to do with the attachment information.

作为我自己的“深入了解”教程,我正在构建一个 PHP 脚本来从 POP3 邮箱收集电子邮件。在尝试使用二进制附件时,我一直试图弄清楚如何处理附件信息。

Given a string that would be gathered from an email:

给定一个将从电子邮件中收集的字符串:

------=_Part_16735_17392833.1229653992102 Content-Type: image/jpeg; name=trans2.jpg Content-Transfer-Encoding: base64 X-Attachment-Id: f_fow87t5j0 Content-Disposition: attachment; filename=trans2.jpg

/9j/4AAQSkZJRgABAgEASABIAAD/4QxrRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUA AAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAIAAAExAAIAAAAUAAAAcgEyAAIAAAAUAAAAhodp

(...)

EAgEAgEAgEAgEAg8IBQRL/Lbe/tJrScHqZ2lkmE4XUP2XcSDZZ2VvZ28dtbsDIYmhkbRxAIJCAQC AQCAQf/ScyAQCAQCAQCAQCAQCAQCAQCAQCAQCAQf/9k= ------=_Part_16735_17392833.1229653992102--

------=_Part_16735_17392833.1229653992102 内容类型:图像/jpeg;name=trans2.jpg 内容传输编码:base64 X-Attachment-Id:f_fow87t5j0 内容处理:附件;文件名=trans2.jpg

/9j/4AAQSkZJRgABAgEASABIAAD/4QxrRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAAUA AAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAIAAAExAAIAAAAUAAAAcgEyAAAIAAAAUAAAAhodp

(……)

EAgEAgEAgEAgEAg8IBQRL/Lbe/tJrScHqZ2lkmE4XUP2XcSDZZ2VvZ28dtbsDIYmhkbRxAIJCAQC AQCAQf/ScyAQCAQCAQCAQCAQCAQCAQCAQCAQCAQf/9k= 6 -------= 3 2 5 3 2 3 2 3 5 3 2 8 3 5 3 0

Is there a way to save off the data to disk so that it would be in a usable format?

有没有办法将数据保存到磁盘,使其成为可用的格式?

回答by Paul Dixon

Pass the data to base64_decode()to get the binary data, write it out to a file with file_put_contents()

将数据传递给base64_decode()以获取二进制数据,使用file_put_contents()将其写入文件

回答by Mike Boers

If you have a lot of data to write out that needs to be decoded before writing I would suggest using stream filters so decode the data as you write it...

如果您有大量数据需要在写入之前进行解码,我建议您使用流过滤器,以便在写入数据时对其进行解码...

$fh = fopen('where_you_are_writing', 'wb');
stream_filter_append($fh, 'convert.base64-decode');

// Do a lot of writing here. It will be automatically decoded from base64.

fclose($h);

回答by Mehdi Karamosly

I tried the below but did not work for me, was generating an empty image file.

我尝试了以下但对我不起作用,正在生成一个空的图像文件。

file_put_contents('img.png', base64_decode($base64string));

this is how it worked for me:

这就是它对我的工作方式:

$data = 'data:image/png;base64,AAAFBfj42Pj4';

list($type, $data) = explode(';', $data);
list(, $data)      = explode(',', $data);
$data = base64_decode($data);

file_put_contents('/tmp/image.png', $data);

I took the code from : How to save a PNG image server-side, from a base64 data string

我从以下代码中获取了代码: How to save a PNG image server-side, from a base64 data string

回答by user2511210

function base64_decode_file($data)
{
    if(preg_match('/^data\:([a-zA-Z]+\/[a-zA-Z]+);base64\,([a-zA-Z0-9\+\/]+\=*)$/', $data, $matches)) {
        return [
                'mime' => $matches[1],
                'data' => base64_decode($matches[2]),
        ];
    }
    return false;
}

回答by Suman

I had a similar situation, and this is what I did. As noted earlier, make sure to remove the extraneous lines before and after the base64 string.

我遇到了类似的情况,这就是我所做的。如前所述,请确保删除 base64 字符串前后的无关行。

<?php
$INPUT = "inputfile_base64_encoded.txt";
$OUTPUT = "output_decoded.zip";
$contents = file_get_contents($INPUT);
$bin = base64_decode($contents);
file_put_contents($OUTPUT, $bin);
?>

回答by smartcoder

Chop off first and last line, base64decodeand save under given filename.

砍掉第一行和最后一行,base64decode并保存在给定的文件名下。

done.

完毕。

回答by Milen A. Radev

While reinventing the wheel has some entertainment and educational value, I would try to resist the temptation: Mailparse, Mail_mimeDecode

虽然重新发明轮子有一些娱乐和教育价值,但我会尽量抵制诱惑:MailparseMail_mimeDecode

回答by esdebon

For big base64 strings from a DB you need use load()

对于来自数据库的大 base64 字符串,您需要使用 load()

$imgdata = $FromDB['BASE64']->load();
$imgdata =  base64_decode($imgdata);
file_put_contents($fileName, $imgdata);