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

2022-01-09 00:00:00 binary base64 php

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

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.

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

Given a string that would be gathered from an email:

--------=_Part_16735_17392833.1229653992102内容类型:图片/jpeg;名称=trans2.jpg内容传输编码:base64X 附件 ID:f_fow87t5j0内容处置:附件;文件名=trans2.jpg

------=_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/4QxrRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAAUAAAABAAAAYgEbAAUAAAABAAAAageEoAAMAAAABAAIAAAExAAIAAAAUAAAAcgEyAAIAAAAUAAAAhodp

/9j/4AAQSkZJRgABAgEASABIAAD/4QxrRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUA AAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAIAAAExAAIAAAAUAAAAcgEyAAIAAAAUAAAAhodp

(...)

EAgEAgEAgEAgEAg8IBQRL/Lbe/tJrScHqZ2lkmE4XUP2XcSDZZ2VvZ28dtbsDIYmhkbRxAIJCAQCAQCAQf/ScyAQCAQCAQCAQCAQCAQCAQCAQCAQCAQf/9k=------=_Part_16735_17392833.1229653992102--

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

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

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

推荐答案

将数据传递给 base64_decode() 到获取二进制数据,使用 file_put_contents()

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

相关文章