PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Tools and Utilities » MIME_Type » Bug #3719

autodetect: If mime_content_type fails, _fileAutoDetect should still be used

Details

Request #3719autodetect: If mime_content_type fails, _fileAutoDetect should still be used
Submitted2005-03-06 06:39 UTC
Fromscragz at hotmail dot com
Assignedcweiske
StatusClosed
PackageMIME_Type
PHP Version4.3.9
OSSuSE Linux 9.2
Roadmaps(Not assigned)

Comments

[2005-03-06 06:39 UTC] scragz at hotmail dot com

Description:
------------
I don't know if something's up with my PHP installation, but when trying to detect the MIME type of a MIDI file, mime_content_type is returning false while _fileAutoDetect is returning "audio/unknown\011".

So, basically I'm proposing that all available tools be attempted until one of them succeeds.

Reproduce code:
---------------
instead of:
if (function_exists('mime_content_type')) {
$type = mime_content_type($file);

maybe:
if (function_exists('mime_content_type') && ($type = mime_content_type($file))) {
// detected

[2006-04-30 14:40 UTC] o dot persson at gmail dot com

I read this in a DEPRECATED note for mime_magic: "This extension is deprecated, please use the fileinfo extension from PECL instead.".

Maybe something like this would work (finfo can be used as a class too):

Index: ../../components/PEAR/MIME/Type.php
===================================================================
--- ../../components/PEAR/MIME/Type.php (revision 682)
+++ ../../components/PEAR/MIME/Type.php (working copy)
@@ -328,14 +328,23 @@
*/
function autoDetect($file, $params = false)
{
- @include_once 'System/Command.php';
- if (function_exists('mime_content_type')) {
+ $type = false;
+ if (function_exists('finfo_open')) {
+ $finfo = finfo_open(FILEINFO_MIME);
+ $type = finfo_file($finfo, $file);
+ finfo_close($finfo);
+ }
+ if ($type === false && function_exists('mime_content_type')) {
$type = mime_content_type($file);
- } else if (class_exists('System_Command')) {
- $type = MIME_Type::_fileAutoDetect($file);
- } else {
- return PEAR::raiseError("Sorry, can't autodetect; you need the mime_magic extension or System_Command and 'file' installed to use this function.");
}
+ if ($type === false) {
+ @include_once 'System/Command.php';
+ if (class_exists('System_Command')) {
+ $type = MIME_Type::_fileAutoDetect($file);
+ } else {
+ return PEAR::raiseError("Sorry, can't autodetect; you need the finfo extension, mime_magic extension or System_Command and 'file' installed to use this function.");
+ }
+ }

// _fileAutoDetect() may have returned an error.
if (PEAR::isError($type)) {