/** * Trys to find the mime type of a file * and returns it, otherwise a $default value will * be returned (application/octet-stream per default) * * $file must be an instance of sfFilebasePluginFile * * @param sfFilebasePluginFile $absolute_path * @param string $default * @return string $mime_type */ public static function getMimeType(sfFilebasePluginFile $file, $default = 'application/octet-stream', $extension = null) { $mime_type = false; $ext = $extension === null ? $file->getExtension() : $extension; // Using file_exists() instead of sfFilebasePluginFile::fileExists() // to avoid recursion issue. sfFilebasePluginFile::fileExists() // calls sfFilebasePlugin::getFilebaseFile() calls // sfFilebaseUtil::getMimeType()... if (file_exists($file->getPathname())) { if (!$file->isReadable()) { throw new sfFilebasePluginException(sprintf('File %s is read protected.', $file->getPathname())); } // 1st step, check magic mimeinfo if (class_exists('finfo')) { $finfo = new finfo(FILEINFO_MIME); $mime_type = $finfo->file($file->getPathname()); } elseif (function_exists('mime_content_type')) { $mime_type = mime_content_type($file->getPathname()); } } if ($mime_type) { if (!empty($ext)) { $ext_mime_type = self::getMimeByExtension($ext, null); if ($ext_mime_type != $mime_type && array_key_exists($mime_type, self::$mime_dependencies) && in_array($ext, self::$mime_dependencies[$mime_type])) { return $ext_mime_type; } } return $mime_type; } // 3. check extension return self::getMimeByExtension($ext, $default); }
/** * Trys to find the mime type of a file * and returns it, otherwise a $default value will * be returned (application/octet-stream per default) * * If $extension isset to <> null, this extension will * be assumed as the given file's native extension (used * by file upload manager where the tmp-file has no extension) * * $file must be an instance of sfFilebasePluginFile * * @param sfFilebasePluginFile $absolute_path * @param string $default * @param string $default mime * @param string $default_extension * @return string $mime_type */ public static function getMimeType(sfFilebasePluginFile $file, $default = 'application/octet-stream', $extension = null) { $mime_type = false; $ext = $extension === null ? $file->getExtension() : $extension; // CALL file_exists() TO AVOID RECURSIONS if (file_exists($file->getPathname())) { if (!$file->isReadable()) { throw new sfFilebasePluginException(sprintf('File %s is read protected.', $file->getPathname())); } // 1st step, check magic mimeinfo if (class_exists('finfo')) { $finfo = new finfo(FILEINFO_MIME, '/usr/share/file/magic'); $mime_type = $finfo->file($file->getPathname()); } if (!$mime_type && function_exists('mime_content_type')) { $mime_type = mime_content_type($file->getPathname()); } } // COMPARE DETECTED MIME TYPE WITH // THE FILE'S EXTENSION BY REGARDING // MIME DEPENDENCIES if ($mime_type) { if (!empty($ext)) { $ext_mime_type = self::getMimeByExtension($ext, null); if ($ext_mime_type != $mime_type && array_key_exists($mime_type, self::$mime_dependencies) && in_array($ext, self::$mime_dependencies[$mime_type])) { return $ext_mime_type; } } return $mime_type; } // 3. check extension return self::getMimeByExtension($ext, $default); }