Added CFileHelper::getMimeType and CFileHelper::getMimeTypeByExtension

This commit is contained in:
qiang.xue
2008-11-15 14:24:48 +00:00
parent f28f466200
commit 87dd239911
6 changed files with 491 additions and 2 deletions

View File

@@ -180,4 +180,51 @@ class CFileHelper
else
return false;
}
/**
* Determines the MIME type of the specified file.
* This method will attempt the following approaches in order:
* <ol>
* <li>finfo</li>
* <li>mime_content_type</li>
* <li>{@link getMimeTypeByExtension}</li>
* </ol>
* @param string the file name.
* @return string the MIME type. Null is returned if the MIME type cannot be determined.
*/
public static function getMimeType($file)
{
if(function_exists('finfo_open'))
{
if($info=finfo_open(FILEINFO_MIME))
return finfo_file($info,$file);
else
return null;
}
if(function_exists('mime_content_type'))
return mime_content_type($file);
return self::getMimeTypeByExtension($file);
}
/**
* Determines the MIME type based on the extension name of the specified file.
* This method will use a local map between extension name and MIME type.
* @param string the file name.
* @return string the MIME type. Null is returned if the MIME type cannot be determined.
*/
public static function getMimeTypeByExtension($file)
{
static $extensions;
if($extensions===null)
$extensions=require(Yii::getPathOfAlias('system.utils.mimeTypes').'.php');
if(($pos=strrpos($file,'.'))!==false)
{
$ext=strtolower(substr($file,$pos+1));
if(isset($extensions[$ext]))
return $extensions[$ext];
}
return null;
}
}