Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 19th, 2012  |  syntax: PHP  |  size: 1.39 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function getMimeType($file_name, $upload_type) {
  2.         $mime_type = '';
  3.         /*
  4.           Fileinfo library might fail to determine the mime type in PHP versions prior to 5.3
  5.           due to troubles locating the magic file. In this case it will return application/octet-stream.
  6.           On some hosting envoronments application/x-character-device is returned for all file types.
  7.           So we will assume these values are incorrect and will keep trying to determine the mime type.
  8.           We will use the FILEINFO_MIME constant instead FILEINFO_MIME_TYPE as the latter exist only in PHP 5.3+
  9.         */
  10.         $bad_mime = array('application/octet-stream', 'application/x-character-device');
  11.         if (!$mime_type && function_exists('finfo_open')) {
  12.                 $fileInfo = finfo_open(FILEINFO_MIME);
  13.                 $mime_type = finfo_file($fileInfo, $file_name);
  14.                 $mime_type = explode(';', $mime_type);
  15.                 $mime_type = trim($mime_type[0]);
  16.                 $finfo_close = ($fileInfo);
  17.         }
  18.         if ((!$mime_type || in_array($mime_type, $bad_mime)) && function_exists('mime_content_type')) {
  19.                 $mime_type = mime_content_type($file_name);
  20.         }
  21.         if (!$mime_type || in_array($mime_type, $bad_mime)) {
  22.                 $cmd = 'file -ib '.escapeshellarg($file_name).' 2> /dev/null';
  23.                 $mime_type = exec($cmd);
  24.                 $mime_type = explode(';', $mime_type);
  25.                 $mime_type = trim($mime_type[0]);
  26.         }
  27.         if ((!$mime_type || in_array($mime_type, $bad_mime)) && $upload_type) {
  28.                 $mime_type = $upload_type;
  29.         }
  30.         return $mime_type;
  31. }