In file_is_text(): mime_content_type is deprecated and not necessarily available
Reported by Fabrice Luraine | July 23rd, 2009 @ 12:12 PM | in Release 0.4 - Fresh Mint And Ginger
The mime_content_type function maybe unavailable; It is also deprecated. So we should use Fileinfo instead. If Fileinfo isn't available, we can provide an alternative with the mime_type() Limonade internal function:
preg_match("/\.([^\.]*?)$/", $filename, $m);
if($mime = mime_type($m[1])) return substr($mime,0,5) == "text/";
Comments and changes to this ticket
-
Fabrice Luraine July 23rd, 2009 @ 12:15 PM
Instead of the above regexp, we can use file_extension() Limoande function
$ext = strtolower(file_extension($filename)); if($mime = mime_type($ext)) return substr($mime,0,5) == "text/";
-
Fabrice Luraine July 23rd, 2009 @ 12:28 PM
The
mime_content_type()
compatibility alternate function must be rewritten to apply above changes. And it should be renamed to avoid confusion and be used even ifmime_content_type()
native function exists.So, replace:
if(!function_exists('mime_content_type')) { /** * Detect MIME Content-type for a file * * @param string $filename Path to the tested file. * @return string */ function mime_content_type($filename) { $ext = strtolower(array_pop(explode('.', $filename))); if($mime = mime_type($ext)) return $mime; elseif (function_exists('finfo_open')) { $finfo = finfo_open(FILEINFO_MIME); $mime = finfo_file($finfo, $filename); finfo_close($finfo); return $mime; } else return 'application/octet-stream'; } }
with something like this:
/** * Detect MIME Content-type for a file * * @param string $filename Path to the tested file. * @return string */ function file_mime_content_type($filename) { $ext = file_extension($filename); /* strtolower isn't necessary */ if($mime = mime_type($ext)) return $mime; elseif (function_exists('finfo_open')) { $finfo = finfo_open(FILEINFO_MIME); $mime = finfo_file($finfo, $filename); finfo_close($finfo); return $mime; } else return 'application/octet-stream'; }
And use it in
file_is_text()
:function file_is_text($filename) { if($mime = file_mime_content_type($filename)) return substr($mime,0,5) == "text/"; return null; }
-
Fabrice Luraine July 31st, 2009 @ 06:44 PM
- State changed from new to resolved
(from [c6714c0f2717b0aa828060b366c9117e66efbfb1]) Fix bug in file_is_text, removes mime_content_type compatibility function and add file_mime_content_type() [#15 state:resolved] http://github.com/sofadesign/limonade/commit/c6714c0f2717b0aa828060...
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป
Limonade is a PHP micro-framework.
People watching this ticket
Referenced by
- 15 In file_is_text(): mime_content_type is deprecated and not necessarily available (from [c6714c0f2717b0aa828060b366c9117e66efbfb1]) Fix bug...