2017-02-10 132 views
0

我正在导入带有附件的电子邮件,然后将它们保存到临时文件夹。问题是,一旦文件被保存到最终目录中,它就没有扩展名。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); 
    } 

} 
+0

什么变量$''附件内容?你能打印吗? –

回答

1

使用pathinfo

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

0

试试这个爆炸的文件名,并获得扩展

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