Commit 6f5aca8d authored by Taylor Otwell's avatar Taylor Otwell

added File::is method for accurately determining file type.

parent 8ccf789b
...@@ -39,7 +39,7 @@ class File { ...@@ -39,7 +39,7 @@ class File {
'dvi' => 'application/x-dvi', 'dvi' => 'application/x-dvi',
'gtar' => 'application/x-gtar', 'gtar' => 'application/x-gtar',
'gz' => 'application/x-gzip', 'gz' => 'application/x-gzip',
'php' => 'application/x-httpd-php', 'php' => array('application/x-httpd-php', 'text/x-php'),
'php4' => 'application/x-httpd-php', 'php4' => 'application/x-httpd-php',
'php3' => 'application/x-httpd-php', 'php3' => 'application/x-httpd-php',
'phtml' => 'application/x-httpd-php', 'phtml' => 'application/x-httpd-php',
...@@ -136,6 +136,25 @@ class File { ...@@ -136,6 +136,25 @@ class File {
return file_put_contents($path, $data, LOCK_EX | FILE_APPEND); return file_put_contents($path, $data, LOCK_EX | FILE_APPEND);
} }
/**
* Determine if a file is a given type.
*
* The Fileinfo PHP extension will be used to determine the MIME
* type of the file. Any extension in the File::$mimes array may
* be passed as a type.
*/
public static function is($extension, $path)
{
if ( ! array_key_exists($extension, static::$mimes))
{
throw new \Exception("File extension [$extension] is unknown. Cannot determine file type.");
}
$mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
return (is_array(static::$mimes[$extension])) ? in_array($mime, static::$mimes[$extension]) : $mime === static::$mimes[$extension];
}
/** /**
* Extract the extension from a file path. * Extract the extension from a file path.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment