如何在PHP中获取文件的内容类型? [英] How to get the content-type of a file in PHP?

查看:133
本文介绍了如何在PHP中获取文件的内容类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP发送带有附件的电子邮件。附件可以是几种不同文件类型(pdf,txt,doc,swf等)中的任何一种。



首先,脚本使用file_get_contents获取文件。 / p>

后来脚本在标题中回显:

 内容类型:<?php echo $ the_content_type; ?取代; name =<?php echo $ the_file_name;?> 

如何为 $ the_content_type 设置正确的值? >

解决方案

我正在使用此功能,其中包括几个回退来补偿旧版本的PHP或简单的错误结果:

  function getFileMimeType($ file){
if(function_exists('finfo_file')){
$ finfo = finfo_open(FILEINFO_MIME_TYPE) ;
$ type = finfo_file($ finfo,$ file);
finfo_close($ finfo);
} else {
require_once'upgradephp / ext / mime.php';
$ type = mime_content_type($ file);


if(!$ type || in_array($ type,array('application / octet-stream','text / plain'))){
$ secondOpinion = exec('file -b --mime-type'。escapeshellarg($ file),$ foo,$ returnCode);
if($ returnCode === 0&& $ secondOpinion){
$ type = $ secondOpinion;



if(!$ type || in_array($ type,array('application / octet-stream','text / plain'))){
require_once'upgradephp / ext / mime.php';
$ exifImageType = exif_imagetype($ file);
if($ exifImageType!== false){
$ type = image_type_to_mime_type($ exifImageType);
}
}

return $ type;
}

它尝试使用较新的PHP finfo 函数。如果这些不可用,它使用 mime_content_type 替代方案,并包括从 Upgrade.php 库以确保存在。如果没有返回任何有用的东西,它将尝试使用操作系统的文件命令。 AFAIK只能在* NIX系统上使用,如果您打算在Windows上使用此功能,您可能需要更改或删除它。如果没有任何效果,它会尝试 exif_imagetype 作为图像的回退。



我已经注意到不同的服务器不同广泛地支持mime类型的功能,而且Upgrade.php mime_content_type 替换是远非完美的。原始版本和Upgrade.php替换的限制 exif_imagetype 功能尽管可靠地工作。如果您只关心图像,您可能只想使用最后一张图片。


I'm using PHP to send an email with an attachment. The attachment could be any of several different file types (pdf, txt, doc, swf, etc).

First, the script gets the file using "file_get_contents".

Later, the script echoes in the header:

Content-Type: <?php echo $the_content_type; ?>; name="<?php echo $the_file_name; ?>"

How to I set the correct value for $the_content_type?

解决方案

I am using this function, which includes several fallbacks to compensate for older versions of PHP or simply bad results:

function getFileMimeType($file) {
    if (function_exists('finfo_file')) {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $type = finfo_file($finfo, $file);
        finfo_close($finfo);
    } else {
        require_once 'upgradephp/ext/mime.php';
        $type = mime_content_type($file);
    }

    if (!$type || in_array($type, array('application/octet-stream', 'text/plain'))) {
        $secondOpinion = exec('file -b --mime-type ' . escapeshellarg($file), $foo, $returnCode);
        if ($returnCode === 0 && $secondOpinion) {
            $type = $secondOpinion;
        }
    }

    if (!$type || in_array($type, array('application/octet-stream', 'text/plain'))) {
        require_once 'upgradephp/ext/mime.php';
        $exifImageType = exif_imagetype($file);
        if ($exifImageType !== false) {
            $type = image_type_to_mime_type($exifImageType);
        }
    }

    return $type;
}

It tries to use the newer PHP finfo functions. If those aren't available, it uses the mime_content_type alternative and includes the drop-in replacement from the Upgrade.php library to make sure this exists. If those didn't return anything useful, it'll try the OS' file command. AFAIK that's only available on *NIX systems, you may want to change that or get rid of it if you plan to use this on Windows. If nothing worked, it tries exif_imagetype as fallback for images only.

I have come to notice that different servers vary widely in their support for the mime type functions, and that the Upgrade.php mime_content_type replacement is far from perfect. The limited exif_imagetype functions, both the original and the Upgrade.php replacement, are working pretty reliably though. If you're only concerned about images, you may only want to use this last one.

这篇关于如何在PHP中获取文件的内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆