PHP - 识别附件文件扩展名

时间:2017-02-10 06:19:12

标签: 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);
    }

}

2 个答案:

答案 0 :(得分:1)

使用pathinfo

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

答案 1 :(得分:0)

尝试爆炸文件名并获取扩展名

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