Armitage2k
Armitage2k

Reputation: 1274

PHP - identify attachment file extension

I am importing emails with attachments and then save them to a temp folder. The problem is that once the file gets saved to it's final directory, it does not have an extension.

How can I get the attachment's file-extension and then append it to the filename?

/* 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);
    }

}

Upvotes: 0

Views: 76

Answers (2)

Arun Kumaresh
Arun Kumaresh

Reputation: 6311

try this explode the filename and get the extension

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

Upvotes: 0

jake2389
jake2389

Reputation: 1174

Use pathinfo

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

Upvotes: 1

Related Questions