PHP Mime类型检查的另一种方法是什么?

10

我注意到get_mime()现在已被弃用,那么有什么创建以下函数的替代方法?由于我使用的是一个CMS,因此需要一个可靠的替代方法。

// Mime Type Checker
function get_mime ($filename,$mode=0) {

    // mode 0 = full check
    // mode 1 = extension check only

    $mime_types = array(

        'txt' => 'text/plain',
        'htm' => 'text/html',
        'html' => 'text/html',
        'php' => 'text/html',
        'css' => 'text/css',
        'js' => 'application/javascript',
        'json' => 'application/json',
        'xml' => 'application/xml',
        'swf' => 'application/x-shockwave-flash',
        'flv' => 'video/x-flv',

        // images
        'png' => 'image/png',
        'jpe' => 'image/jpeg',
        'jpeg' => 'image/jpeg',
        'jpg' => 'image/jpeg',
        'gif' => 'image/gif',
        'bmp' => 'image/bmp',
        'ico' => 'image/vnd.microsoft.icon',
        'tiff' => 'image/tiff',
        'tif' => 'image/tiff',
        'svg' => 'image/svg+xml',
        'svgz' => 'image/svg+xml',

        // archives
        'zip' => 'application/zip',
        'rar' => 'application/x-rar-compressed',
        'exe' => 'application/x-msdownload',
        'msi' => 'application/x-msdownload',
        'cab' => 'application/vnd.ms-cab-compressed',

        // audio/video
        'mp3' => 'audio/mpeg',
        'qt' => 'video/quicktime',
        'mov' => 'video/quicktime',

        // adobe
        'pdf' => 'application/pdf',
        'psd' => 'image/vnd.adobe.photoshop',
        'ai' => 'application/postscript',
        'eps' => 'application/postscript',
        'ps' => 'application/postscript',

        // ms office
        'doc' => 'application/msword',
        'rtf' => 'application/rtf',
        'xls' => 'application/vnd.ms-excel',
        'ppt' => 'application/vnd.ms-powerpoint',
        'docx' => 'application/msword',
        'xlsx' => 'application/vnd.ms-excel',
        'pptx' => 'application/vnd.ms-powerpoint',


        // open office
        'odt' => 'application/vnd.oasis.opendocument.text',
        'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
    );

    $ext = strtolower(array_pop(explode('.',$filename)));

    if (function_exists('mime_content_type') && $mode==0) {
        $mimetype = mime_content_type($filename);
        return $mimetype;
    }

    if (function_exists('finfo_open') && $mode==0) {
        $finfo = finfo_open(FILEINFO_MIME);
        $mimetype = finfo_file($finfo, $filename);
        finfo_close($finfo);
        return $mimetype;

    } elseif (array_key_exists($ext, $mime_types)) {
        return $mime_types[$ext];
    } else {
        return 'application/octet-stream';
    }
}

尝试使用文件信息函数。也许你可以通过http://www.php.net/manual/en/ref.fileinfo.php获得结果。 - jeni
3个回答

11

finfo_filemime_content_type 的替代品。

由于 finfo_file 仅在 PHP 5.3.0 及以上版本中可用,因此您所做的是正确的。如果 finfo_file 可用,则使用它,否则回退到 mime_content_type,如果无法使用 finfo_file,则应该可用 mime_content_type。

您应该像这样更新您的条件检查:

if(function_exists('mime_content_type')&&$mode==0){ 
        $mimetype = mime_content_type($filename); 
        return $mimetype; 

}elseif(function_exists('finfo_open')&&$mode==0){ 
        $finfo = finfo_open(FILEINFO_MIME); 
        $mimetype = finfo_file($finfo, $filename); 
        finfo_close($finfo); 
        return $mimetype; 
}elseif(array_key_exists($ext, $mime_types)){ 
        return $mime_types[$ext]; 
}else { 
        return 'application/octet-stream'; 
} 

1

我之前用过类似的东西,只要你的PHP版本> 5.3.0,它似乎可以正常工作:

$fn = "/dir/filename.whatever"
$mime = finfo_open(FILEINFO_MIME,$mimepath);
$filetype = finfo_file($mime,$fn);
finfo_close($mime);

注意:我记得$mimepath是一个未设置的变量。这将导致finfo_open()使用其第二个参数的默认值。

2
请注意,此代码可能返回如“video/x-flv; charset=binary”等字符串,您可能不需要“charset=binary”部分。如果您只想返回MIME类型,建议将以下行添加到上述代码的末尾:$ mimetype = substr($ mimetype,0,strpos($mimetype,';')); - Andrew

0
$mime = `file --mime-type $path`;
$mime = trim(array_pop(explode(':', $mime)));

这对我有效。


1
file -b --mime-type $path 可以直接使用,无需 trim(array_pop(explode(':', $mime))); - Richard Ayotte
5
抱歉打扰,但这种方法存在真正的安全问题需要注意。这些勾号会执行一个shell命令,因此您需要严格清理$path,如果您想用这种方式进行mime检查。此外,您正在使用一个POSIX命令,该命令不能保证在系统上存在(例如Windows、自定义Linux发行版或其他操作系统)。 - J. A. Streich

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接