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

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

问题描述

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

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).

首先,脚本使用file_get_contents"获取文件.

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; ?>"

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

How to I set the correct value for $the_content_type?

推荐答案

我正在使用这个函数,它包括几个回退来补偿旧版本的 PHP 或只是糟糕的结果:

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;
}

它尝试使用较新的 PHP finfo 函数.如果这些不可用,它会使用 mime_content_type 替代方案,并包括来自 的直接替换Upgrade.php 库以确保它存在.如果这些没有返回任何有用的东西,它会尝试操作系统的 file 命令.AFAIK 仅在 *NIX 系统上可用,如果您打算在 Windows 上使用它,您可能需要更改或摆脱它.如果没有任何效果,它会尝试 exif_imagetype 作为仅图像的后备.

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.

我注意到不同的服务器对 mime 类型函数的支持差异很大,并且 Upgrade.php mime_content_type 替代品远非完美.有限的 exif_imagetype 函数,无论是原始函数还是 Upgrade.php 替换函数,都非常可靠地工作.如果您只关心图片,您可能只想使用最后一张.

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天全站免登陆