PHP - 识别附件文件扩展名



我正在导入带有附件的电子邮件,然后将它们保存到临时文件夹中。问题是,一旦文件保存到它的最终目录,它就没有扩展名。

如何获取附件的文件扩展名,然后将其附加到文件名中?

/* iterate through each attachment and save it */
foreach($attachments as $attachment)
{
    if($attachment['is_attachment'] == 1)
    {
        $filename = $overview[0]->subject;
        if(empty($filename)) $filename = $attachment['filename'];
        if(empty($filename)) $filename = time() . ".dat";
        /* prefix the email number to the filename in case two emails
         * have the attachment with the same file name.
         */
        $fp = fopen('./'.$holidex.'/'.$email_number."-".$filename, "w+");
        fwrite($fp, $attachment['attachment']);
        fclose($fp);
    }
}

使用 pathinfo

$file_ext = ".".pathinfo($file['name'], PATHINFO_EXTENSION);

尝试这样做 分解文件名并获取扩展名

$temp = explode(".", $attachment['filename']);
$extension=end($temp);

最新更新