티스토리 뷰

참고 : http://blog.habonyphp.com/4#.U5M2pGdZryF

mime_content_type (PHP 4 >= 4.3.0, PHP 5)

이 함수는 단순히 파일의 확장자를 기초로 파일타입을 출력합니다.
예로 myimage.bmp가 있다면 실제 그림타입이 jpg이지만 bmp타입을 출력할 것입니다.

string mime_content_type ( string $filename )


 <?php 
  function user_mime_content_type($filename) 
  { 
    if(!function_exists("mime_content_type")) 
    { 
       $type = array( 
       "txt" => "text/plain", 
       "htm" => "text/html", 
       "html" => "text/html", 
       "php" => "text/html", 
       "css" => "text/css", 
       "js" => "application/javascript", 
       "json" => "application/json", 
       "xml" => "application/xml", 
       "swf" => "application/x-shockwave-flash", 
       "flv" => "video/x-flv", 

      // images 
       "png" => "image/png", 
       "jpe" => "image/jpeg", 
       "jpeg" => "image/jpeg", 
       "jpg" => "image/jpeg", 
       "gif" => "image/gif", 
       "bmp" => "image/bmp", 
       "ico" => "image/vnd.microsoft.icon", 
       "tiff" => "image/tiff", 
       "tif" => "image/tiff", 
       "svg" => "image/svg+xml", 
       "svgz" => "image/svg+xml", 
       
       // archives 
       "zip" => "application/zip", 
       "rar" => "application/x-rar-compressed", 
       "exe" => "application/x-msdownload", 
       "msi" => "application/x-msdownload", 
       "cab" => "application/vnd.ms-cab-compressed", 
       
       // audio/video 
       "mp3" => "audio/mpeg", 
       "qt" => "video/quicktime", 
        "mov" => "video/quicktime", 
      
       // adobe 
       "pdf" => "application/pdf", 
       "psd" => "image/vnd.adobe.photoshop", 
       "ai" => "application/postscript", 
       "eps" => "application/postscript", 
       "ps" => "application/postscript", 

      // ms office 
       "doc" => "application/msword", 
       "rtf" => "application/rtf", 
       "xls" => "application/vnd.ms-excel", 
       "ppt" => "application/vnd.ms-powerpoint", 
      
       // open office 
       "odt"=>"application/vnd.oasis.opendocument.text", 
       "ods"=>"application/vnd.oasis.opendocument.spreadsheet", 
      ); 
      $ext = strtolower(array_pop(explode(".",$filename))); 
      if (array_key_exists($ext, $type)) 
      { 
          return $type[$ext]; 
       } 
       elseif (function_exists("finfo_open")) 
      { 
          $finfo = finfo_open(FILEINFO_MIME); 
          $mimetype = finfo_file($finfo, $filename); 
          finfo_close($finfo); 
          return $mimetype; 
      } 
      else 
     { 
         return "application/octet-stream"; 
     } 
   } 
   else 
   { 
       return mime_content_type($filename); 
    } 
  } 

 ?> 
 

예제 (ex #1

 <?php 
   $type = user_mime_content_type("test.bmp.txt.zip.jpg"); 
  print_r($type); // 결과: image/jpeg 
 ?>  

예제 (ex #2

 <?php 
  $type = user_mime_content_type("test.jpg"); 
  header("Content-type: ".$type); 
  header("content-disposition: attachment; filename="test.jpg""); 
  header("content-description: php generated data"); 
  header("Pragma: no-cache");  
  header("Expires: 0");  

 $fp = fopen("test.jpg", "r"); 
  if (!fpassthru($fp)) 
  { 
    fclose($fp); 
  } 
  @flush(); 
 ?>  

'Language > PHP' 카테고리의 다른 글

Board Num  (0) 2015.11.29
PHP로 브라우저 확인하기  (0) 2015.11.29
HTML to PDF - mPDF  (0) 2015.11.29
DB 연결 - dbconn.inc  (0) 2015.11.29
XML 문서파싱 - SAX 방식, DOM 방식  (0) 2015.11.29
Total
Today
Yesterday
최근에 올라온 글
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31