示例#1
0
 /**
  * Sends a content type header
  *
  * @param string $mime
  * @param string $charset
  * @param boolean $send
  * @return mixed
  */
 public static function contentType($mime, $charset = 'UTF-8', $send = true)
 {
     if (f::extensionToMime($mime)) {
         $mime = f::extensionToMime($mime);
     }
     $header = 'Content-type: ' . $mime;
     if ($charset) {
         $header .= '; charset=' . $charset;
     }
     if (!$send) {
         return $header;
     }
     header($header);
 }
示例#2
0
 /**
  * Returns the mime type of a file
  *
  * @param string $file
  * @return mixed
  */
 public static function mime($file)
 {
     // stop for invalid files
     if (!file_exists($file)) {
         return null;
     }
     // Fileinfo is prefered if available
     if (function_exists('finfo_file')) {
         $finfo = finfo_open(FILEINFO_MIME_TYPE);
         $mime = finfo_file($finfo, $file);
         finfo_close($finfo);
         return $mime;
     }
     // for older versions with mime_content_type go for that.
     if (function_exists('mime_content_type') && ($mime = @mime_content_type($file) !== false)) {
         return $mime;
     }
     // shell check
     try {
         $mime = system::execute('file', [$file, '-z', '-b', '--mime'], 'output');
         $mime = trim(str::split($mime, ';')[0]);
         if (f::mimeToExtension($mime)) {
             return $mime;
         }
     } catch (Exception $e) {
         // no mime type detectable with shell
         $mime = null;
     }
     // Mime Sniffing
     $reader = new MimeReader($file);
     $mime = $reader->get_type();
     if (!empty($mime) && f::mimeToExtension($mime)) {
         return $mime;
     }
     // guess the matching mime type by extension
     return f::extensionToMime(f::extension($file));
 }