protected function detectFileExtension($path)
{
static $type, $finfo, $volume;
if (!$type) {
$keys = array_keys($this->volumes);
$volume = $this->volumes[$keys[0]];
if (class_exists('finfo', false)) {
$tmpFileInfo = explode(';', finfo_file(finfo_open(FILEINFO_MIME), __FILE__));
} else {
$tmpFileInfo = false;
}
$regexp = '/text\\/x\\-(php|c\\+\\+)/';
if ($tmpFileInfo && preg_match($regexp, array_shift($tmpFileInfo))) {
$type = 'finfo';
$finfo = finfo_open(FILEINFO_MIME);
} elseif (function_exists('mime_content_type') && preg_match($regexp, array_shift(explode(';', mime_content_type(__FILE__))))) {
$type = 'mime_content_type';
} elseif (function_exists('getimagesize')) {
$type = 'getimagesize';
} else {
$type = 'none';
}
}
$mime = '';
if ($type === 'finfo') {
$mime = finfo_file($finfo, $path);
} elseif ($type === 'mime_content_type') {
$mime = mime_content_type($path);
} elseif ($type === 'getimagesize') {
if ($img = getimagesize($path)) {
$mime = $img['mime'];
}
}
if ($mime) {
$mime = explode(';', $mime);
$mime = trim($mime[0]);
if (in_array($mime, array('application/x-empty', 'inode/x-empty'))) {
// finfo return this mime for empty files
$mime = 'text/plain';
} elseif ($mime == 'application/x-zip') {
// http://elrte.org/redmine/issues/163
$mime = 'application/zip';
}
}
$ext = $mime ? $volume->getExtentionByMime($mime) : '';
return $ext ? '.' . $ext : '';
}