【问题标题】:PHP Extract binary data from headerPHP 从标头中提取二进制数据
【发布时间】:2022-01-09 16:27:41
【问题描述】:

我正在使用我的 PHP 来提取我将电子邮件标头存储在 mysql 数据库中的二进制数据。我需要一些帮助来提取二进制数据以获取附件二进制数据。

例子:

UEsDBBQAAAAIAEpZrEjPyoXURw....etc

如果您要查找文件名,我不知道如何提取二进制数据,例如:email_example1.zip。

这是标题:

Return-Path: <sender@domain.com>
Delivered-To: chris@domain.com
Received: from domain.com

------=_Part_4094373_1508330616.1564422111167
Content-Type: application/zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="email_example1.zip"
Content-ID: <05063d19-5033-af14-87e2-d2fbf22d5857@yahoo.com>

UEsDBBQAAAAIAEpZrEjPyoXURw....etc


------=_Part_4094373_1508330616.1564422111167
Content-Type: application/zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="example2.zip"
Content-ID: <3b2c4fee-2b28-778b-b27f-c63881d64e17@domain.com>

UEsDBBQAAAAIALtk6U5W+XzU7iM....etc

这里是 PHP:

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

//Connect to the database
include('config.php');

$id = $_GET['id'];
$attid = $_GET['attid'];
$message_id = $_GET['msgid'];
$mailbox_sql = 'SELECT * FROM ' . $mailfolder . ' WHERE email_id = ? AND message_id = ?';
$mailbox = $link->prepare($mailbox_sql);
$mailbox->execute([$id,$message_id]); 

// set the resulting array to associative
$row = $mailbox->fetch(PDO::FETCH_ASSOC);


if (is_array($row)) {
   $attached = $row['attached_files'];
   $attached_arr = explode("\n", $attached);
}

foreach ($attached_arr as $files) {
    $attached_file = 'attid: ' . $attid . ' filename:';
    $attached = '';

    if (strpos($files,  ' attid: ') !== false) {
        $filename = trim(strrchr($files, ':'), ': ');
        $files = 'attid: ' . $attid . ' filename: ' . $filename;
    }


    if (strpos($files, $attached_file) !== false) {
       $attached = trim(strrchr($files, ':'), ': ');
    }

mailbox = null;
?>

你知道我如何搜索电子邮件标题中的文件名来提取二进制数据吗?

任何建议都会非常感激。

【问题讨论】:

  • 那是某种编码,也许是base64?
  • @user3783243 是的,我想提取编码数据。你知道怎么做吗??
  • @bumperbox 我知道如何使用 base64_decode 但我正在谈论如何搜索 base64 编码字符串,例如:UEsDBBQAAAAIAEpZrEjPyoXURw....etc。你知道我在搜索文件名时如何获取base64编码字符串吗??
  • 二进制数据在您的正文中 Content-Type: application/zip 。文件名通过 Content-Disposition Header Content-Disposition: attachment; filename="email_example1.zip" 公开,您只需解析文件名即可。如果这不能回答您的问题,请更好地解释您想要实现的目标。

标签: php email-attachments


【解决方案1】:

您需要直接从正文中获取输入流,因为您的内容是 application/zip Content-Type: application/zip

为此,您可以使用php://input

$input = file_get_contents('php://input');
$binary = base64ToBinary($input);

function base64ToBinary(string $string): string {
    if (strpos($string, ';base64,') !== false) {
        $string = explode(';base64,', $string)[1];
    }

    return base64_decode($string);
}

文件名通过 Content-Disposition Header 公开: 在您的帖子中,您有Content-Disposition: attachment; filename="email_example1.zip",您可以从那里解析它。

编辑

如何从 Header 中解析文件名:

$value = $_SERVER['HTTP_CONTENT_DISPOSITION'];
$filename = 'default_name.zip';

if (preg_match('/filename="-(.*?)-"/', $value, $match) == 1) {
    $filename = $match[1];
}

我提供的代码 100% 适用于您提供的 Content Disposition Header:

如果它对您不起作用,则意味着您发布的格式有误,请在 how to get file name from content disposition header 上查看此问题以获取答案。

否则,您的请求与解析字符串中的值的二进制数据无关。请更新您的问题描述以匹配您想要实现的目标。

【讨论】:

  • 谢谢,但这不是我要找的答案。就像我在之前的评论中所说的那样,我想解析文件名以获取 base64 代码,例如 UEsDBBQAAAAIAEpZrEjPyoXURw....etc。现在清楚了吗??
  • 您在答案上发布的代码只是从 base64 转换为二进制。我正在寻找如何解析文件名以获取 base64 字符串。
  • 应该是这样的$str = 'filename="example2.zip"'; preg_match('/^.*?filename=(["\'])1/m', $str, $matches); print_r($matches);
  • @chrisoojer 检查我的更新答案。
  • 谢谢,但如果 preg_match 没有传递。请再次检查。
猜你喜欢
  • 2018-01-26
  • 1970-01-01
  • 1970-01-01
  • 2015-04-23
  • 2017-09-15
  • 2016-01-10
  • 1970-01-01
  • 2014-02-15
相关资源
最近更新 更多