Fixing a couple of porting bugs in file::mime()
This commit is contained in:
parent
4bd2ede0e8
commit
c2365495fe
|
@ -12,67 +12,56 @@ class file_Core {
|
||||||
/**
|
/**
|
||||||
* Attempt to get the mime type from a file. This method is horribly
|
* Attempt to get the mime type from a file. This method is horribly
|
||||||
* unreliable, due to PHP being horribly unreliable when it comes to
|
* unreliable, due to PHP being horribly unreliable when it comes to
|
||||||
* determining the mime-type of a file.
|
* determining the mime type of a file.
|
||||||
*
|
*
|
||||||
* @param string filename
|
* @param string file path
|
||||||
* @return string mime-type, if found
|
* @return string mime type, on success
|
||||||
* @return boolean FALSE, if not found
|
* @return FALSE on failure
|
||||||
*/
|
*/
|
||||||
public static function mime($filename)
|
public static function mime($filename)
|
||||||
{
|
{
|
||||||
// Make sure the file is readable
|
// Get the complete path to the file
|
||||||
if ( ! (is_file($filename) AND is_readable($filename)))
|
$filename = realpath($filename);
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
// Get the extension from the filename
|
// Get the extension from the filename
|
||||||
$extension = strtolower(substr(strrchr($filename, '.'), 1));
|
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
|
||||||
|
|
||||||
if (preg_match('/^(?:jpe?g|png|[gt]if|bmp|swf)$/', $extension))
|
if (preg_match('/^(?:jpe?g|png|[gt]if|bmp|swf)$/', $extension))
|
||||||
{
|
{
|
||||||
// Disable error reporting
|
|
||||||
$ER = error_reporting(0);
|
|
||||||
|
|
||||||
// Use getimagesize() to find the mime type on images
|
// Use getimagesize() to find the mime type on images
|
||||||
$mime = getimagesize($filename);
|
$file = getimagesize($filename);
|
||||||
|
|
||||||
// Turn error reporting back on
|
if (isset($image['mime']))
|
||||||
error_reporting($ER);
|
return $file['mime'];
|
||||||
|
|
||||||
// Return the mime type
|
|
||||||
if (isset($mime['mime']))
|
|
||||||
return $mime['mime'];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (function_exists('finfo_open'))
|
if (function_exists('finfo_open'))
|
||||||
{
|
{
|
||||||
// Use the fileinfo extension
|
if ($file = finfo_open(FILEINFO_MIME))
|
||||||
$finfo = finfo_open(FILEINFO_MIME);
|
{
|
||||||
$mime = finfo_file($finfo, $filename);
|
// Get the mime type
|
||||||
finfo_close($finfo);
|
$mime = finfo_file($file, $filename);
|
||||||
|
|
||||||
|
// Close the finfo
|
||||||
|
finfo_close($file);
|
||||||
|
}
|
||||||
|
|
||||||
// Return the mime type
|
|
||||||
return $mime;
|
return $mime;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ini_get('mime_magic.magicfile') AND function_exists('mime_content_type'))
|
if (ini_get('mime_magic.magicfile') AND function_exists('mime_content_type'))
|
||||||
{
|
{
|
||||||
// Return the mime type using mime_content_type
|
// The mime_content_type function is only useful with a magic file
|
||||||
return mime_content_type($filename);
|
return mime_content_type($filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! KOHANA_IS_WIN)
|
if ( ! empty($extension))
|
||||||
{
|
{
|
||||||
// Attempt to locate use the file command, checking the return value
|
// Guess the mime using the file extension
|
||||||
if ($command = trim(exec('which file', $output, $return)) AND $return === 0)
|
$mimes = Kohana::config('mimes');
|
||||||
{
|
|
||||||
return trim(exec($command.' -bi '.escapeshellarg($filename)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( ! empty($extension) AND is_array($mime = Kohana::config('mimes.'.$extension)))
|
if (isset($mimes[$extension]))
|
||||||
{
|
return $mimes[$extension][0];
|
||||||
// Return the mime-type guess, based on the extension
|
|
||||||
return $mime[0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unable to find the mime-type
|
// Unable to find the mime-type
|
||||||
|
|
Loading…
Reference in a new issue