コード例 #1
0
ファイル: mime_type.php プロジェクト: essemme/media
 /**
  * Guesses the MIME type of the file
  *
  * Empty results are currently not handled:
  * 	application/x-empty
  * 	application/x-not-regular-file
  *
  * @param string $file
  * @param options $options Valid options are:
  *	- `'paranoid'` If set to true only then content for the file is used for detection
  *	- `'properties'` Used for simplification, defaults to false
  *	- `'experimental'` Used for simplification, defaults to false
  * @return mixed string with MIME type on success
  * @access public
  */
 function guessType($file, $options = array())
 {
     $_this =& MimeType::getInstance();
     $defaults = array('paranoid' => false, 'properties' => false, 'experimental' => true);
     extract($options + $defaults);
     $magicMatch = $globMatch = array();
     if (!$paranoid) {
         if (is_a($_this->__glob, 'MimeGlob')) {
             $globMatch = $_this->__glob->analyze($file);
         }
         if (count($globMatch) === 1) {
             return MimeType::simplify(array_shift($globMatch), $properties, $experimental);
         }
     }
     if (!is_readable($file)) {
         return null;
     }
     if (is_a($_this->__magic, 'finfo')) {
         $magicMatch = $_this->__magic->file($file);
     } elseif ($_this->__magic === 'mime_magic') {
         $magicMatch = mime_content_type($file);
     } elseif (is_a($_this->__magic, 'MimeMagic')) {
         $magicMatch = $_this->__magic->analyze($file);
     }
     $magicMatch = empty($magicMatch) ? array() : array($magicMatch);
     if (empty($magicMatch)) {
         $File =& new File($file);
         if (preg_match('/[\\t\\n\\r]+/', $File->read(32))) {
             return 'text/plain';
         }
         return 'application/octet-stream';
     }
     if (count($magicMatch) === 1) {
         return MimeType::simplify(array_shift($magicMatch), $properties, $experimental);
     }
     if ($globMatch && $magicMatch) {
         $combinedMatch = array_intersect($globMatch, $magicMatch);
         if (count($combinedMatch) === 1) {
             return MimeType::simplify(array_shift($combinedMatch), $properties, $experimental);
         }
     }
     return null;
 }