如果PHP IMAP的文件名包含星号,则无法获取附件

如果PHP IMAP的文件名包含星号,则无法获取附件,php,email,pdf,imap,Php,Email,Pdf,Imap,我正在使用IMAP通过读取邮箱来进行一些数据输入。除了附件文件名包含星号(*)字符的一个实例外,该代码几乎适用于所有电子邮件,在这种情况下,代码无法获取文件(或者更准确地说,PDF附件不可读) 我为获取电子邮件内容(包括获取PDF附件)而编写的代码是: 尽管如此,我仍然可以将所有信息(标题、文件名等)保存到数据库中,但由于某种原因,只有保存到服务器上的文件是不可读的(见附图) 以前有人遇到过这种情况吗 您是否使用传统的电子邮件客户端下载附件并确保其格式正确?目前我正在使用cPanel webma

我正在使用IMAP通过读取邮箱来进行一些数据输入。除了附件文件名包含星号(*)字符的一个实例外,该代码几乎适用于所有电子邮件,在这种情况下,代码无法获取文件(或者更准确地说,PDF附件不可读)

我为获取电子邮件内容(包括获取PDF附件)而编写的代码是:

尽管如此,我仍然可以将所有信息(标题、文件名等)保存到数据库中,但由于某种原因,只有保存到服务器上的文件是不可读的(见附图)

以前有人遇到过这种情况吗


您是否使用传统的电子邮件客户端下载附件并确保其格式正确?目前我正在使用cPanel webmail(Horde)下载电子邮件。有趣的是,当它下载到我的Windows计算机上时,文件名会自动从
*MEA-发票号91135811*
更改为
\u MEA-发票号91139748
(星号改为下划线),然后可读。我假设IMAP无法解析的文件名有问题?您是否使用传统的电子邮件客户端下载附件并确保其格式正确?目前我正在使用cPanel webmail(Horde)下载电子邮件。有趣的是,当它下载到我的Windows计算机上时,文件名会自动从
*MEA-发票号91135811*
更改为
\u MEA-发票号91139748
(星号改为下划线),然后可读。我假设IMAP无法解析的文件名有问题?
public function fetchBody ($message_num) {
    if ($this->imap) {
        $body = '';
        $body_type = 'text';
        $attachments = array();
        $structure = imap_fetchstructure($this->imap, $message_num);

        //pr($structure);

        if (!$structure) {
            return false;
        } else {
            if ($structure->type == 0) {
                if (strtolower($structure->subtype) == 'html') {
                    $body_type = 'html';
                }
                $body = $this->decodeBody(imap_body($this->imap, $message_num), $structure->encoding, FT_PEEK);
            } elseif ($structure->type == 1) {
                // Grab the text portion of a multipart email
                if (count($structure->parts)) {
                    foreach ($structure->parts as $i => $part) {
                        if (strtolower($part->subtype) == 'alternative') {
                            if (count($part->parts)) {
                                foreach ($part->parts as $j => $subpart) {
                                    if (strtolower($subpart->subtype) == 'plain' || strtolower($subpart->subtype) == 'text') {
                                        $body = $this->decodeBody(imap_fetchbody($this->imap, $message_num, ($i + 1) . '.' . ($j + 1), FT_PEEK), $subpart->encoding);
                                    }
                                }
                            }
                        } elseif (strtolower($part->subtype) == 'related') {
                            if (count($part->parts)) {
                                foreach ($part->parts as $j => $subpart) {
                                    if ( isset($subpart->parts) && count($subpart->parts)) {
                                        foreach ($subpart->parts as $k => $subsubpart) {
                                            if (strtolower($subsubpart->subtype) == 'plain' || strtolower($subsubpart->subtype) == 'text') {
                                                $body = $this->decodeBody(imap_fetchbody($this->imap, $message_num, ($i + 1) . '.' . ($j + 1) . '.' . ($k + 1), FT_PEEK), $subsubpart->encoding);
                                            }
                                        }
                                    }
                                }
                            }
                        } elseif (strtolower($part->subtype) == 'plain' || strtolower($part->subtype) == 'text') {
                            $body = $this->decodeBody(imap_fetchbody($this->imap, $message_num, $i + 1, FT_PEEK), $structure->encoding);
                        } else {
                            CakeLog::write('debug', print_r($part, true));
                            if (($part->type >= 2 && $part->type <= 7) && (!isset($part->parts))) {
                                $attachments[] = $this->processAttachment($part, $i);
                            }elseif (strtolower($part->subtype) == 'mixed' || (count($part->parts) && $part->subtype == 'RFC822')) {   

                                if (count($part->parts)) {
                                    foreach ($part->parts as $j => $subpart) {                                            
                                        if (strtolower($subpart->subtype) == 'plain' || strtolower($subpart->subtype) == 'text') {
                                            $body = $this->decodeBody(imap_fetchbody($this->imap, $message_num, ($i + 1) . '.' . ($j + 1) . '.' . ($k + 1), FT_PEEK), $mixedsubpart->encoding);
                                        }else{
                                            if ($subpart->type >= 2 && $subpart->type <= 7) {
                                                $attachments[] = $this->processAttachment($subpart, $i);
                                            }
                                        }                                            
                                    }
                                }                                
                            }
                        }
                    }
                }
            }
        }

        $data['body'] = $this->cleanUp($body, $structure);
        $data['body_type'] = $body_type;
        $data['attachments'] = $attachments;

        return $data;
    } else {
        return false;
    }
}
2016-02-19 17:13:46 Debug: stdClass Object
(
    [type] => 2
    [encoding] => 0
    [ifsubtype] => 1
    [subtype] => RFC822
    [ifdescription] => 0
    [ifid] => 0
    [lines] => 343
    [bytes] => 25561
    [ifdisposition] => 0
    [ifdparameters] => 0
    [ifparameters] => 0
    [parameters] => stdClass Object
        (
        )

    [parts] => Array
        (
            [0] => stdClass Object
                (
                    [type] => 3
                    [encoding] => 3
                    [ifsubtype] => 1
                    [subtype] => PDF
                    [ifdescription] => 1
                    [description] => *MEA - Invoice No. 91135811 *
                    [ifid] => 0
                    [bytes] => 23602
                    [ifdisposition] => 0
                    [ifdparameters] => 0
                    [ifparameters] => 1
                    [parameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => name
                                    [value] => *MEA - Invoice No. 91135811 *.pdf
                                )

                        )

                )

        )

)