2014-07-14 4 views
0

서버에서 파일 쓰루 연결을 보내는 전자 메일 스크립트가 있습니다. 이 같은 코드에 이메일을 phpmailer를 사용하여 전송되고 첨부 파일이 첨부 :PHP + 이미지 유형 감지

$_img = (object) ($img); 
if (!empty($_img->src)) { 
    $ext = substr($_img->src, -3); 

    $imginfo_array = getimagesize($_img->src); 
    $mime_type = $imginfo_array['mime'];      

    switch($mime_type) { 
     case "image/jpeg": 
      $type = "image/jpeg"; 
      break; 
     case "image/png": 
      $type = "image/png"; 
      break; 
     case "image/gif": 
      $type = "image/gif"; 
      break;         
    } 
    $string = file_get_contents($_img->src); 
    $mail->AddStringAttachment($string, $i . '.' . $ext, 'base64', $type); 
} 

문제는 이미지가 제대로 서버에 추가하기 전에 저장되지 않을 때 발생합니다. 한 사용자가 'test.jpg'파일을 'test.png'이라고 결정하면 이메일을 통해 첨부 된 파일을 볼 수 없습니다.

$_img->src은 서버에 저장된 파일입니다.

MIME 형식을 확인하려고하지만 여전히 성공하지 못했습니다. 올바른 파일 유형이 확장 프로그램에 의해 결정되지 않은 자동 감지 유형임을 스크립트에 알리고 싶습니다. 누군가이 작업을 수행하는 방법에 대한 단서를 줄 수 있습니까?

답변

0
 $_img = (object) ($img); 
     if (!empty($_img->src)) { 

      //$ext = substr($_img->src, -3); 
      // better solution for get file extension (with variable length: "jpeg" or "jpeg") 
      $ext = pathinfo($_img->src, PATHINFO_EXTENSION); 

      $imginfo_array = getimagesize($_img->src); 
      $mime_type = $imginfo_array['mime'];      

      switch($mime_type) { 
       case "image/jpeg": 
        $type = "image/jpeg"; 
        $ext = 'jpg'; 
        break; 
       case "image/png": 
        $type = "image/png"; 
        $ext = 'png'; 
        break; 
       case "image/gif": 
        $type = "image/gif"; 
        $ext = 'gif'; 
        break; 
       // fix for others mime type   
       default:  
        $type = "application/octet-stream"; 

      }    

      // for binary file use AddAttachment() 
      //$string = file_get_contents($_img->src);     
      //$mail->AddStringAttachment($string, $i . '.' . $ext, 'base64', $type); 

      // I hope that the variable $i is set from previous code    
      $mail->AddAttachment($_img->src, $i . '.' . $ext, 'base64', $type) 
     } 
+0

제공 대답은 날 전혀 도움이되지 않는 수정/제안 – ToBe

+0

매우 도움이 될 것입니다 무엇인지에 대한 의견 , 실제로는 첨부 파일이있는 메일도 보내지 않습니다. 첨부 파일을 보낸 사람은 내용을 가져올 때만 지원됩니다. – rosuandreimihai

+0

문제가 있습니다. 내 코드는 파일을 보내지 만 표시 할 수는 없습니다. – rosuandreimihai