22

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

首先,脚本使用“file_get_contents”获取文件。

稍后,脚本在标题中回显:

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

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

4

9 回答 9

29

我正在使用这个函数,它包括几个回退来补偿旧版本的 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;
}

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

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

于 2010-09-08T04:35:43.027 回答
11

在 php 中使用它非常容易。

只需调用以下php函数mime_content_type

<?php
    $filelink= 'uploads/some_file.pdf';
    $the_content_type = "";

    // check if the file exist before
    if(is_file($file_link)) {
        $the_content_type = mime_content_type($file_link);
    }
    // You can now use it here.

?>

函数 mime_content_type() 的 PHP 文档 希望对某人有所帮助

于 2016-07-20T08:27:49.933 回答
6

使用 finfo_file:http ://us2.php.net/manual/en/function.finfo-file.php

于 2009-08-05T11:55:41.220 回答
4

这是一个使用PHP5 和 PECL 中可用的finfo_open的示例:

$mimepath='/usr/share/magic'; // may differ depending on your machine
// try /usr/share/file/magic if it doesn't work
$mime = finfo_open(FILEINFO_MIME,$mimepath);
if ($mime===FALSE) {
 throw new Exception('Unable to open finfo');
}
$filetype = finfo_file($mime,$tmpFileName);
finfo_close($mime);
if ($filetype===FALSE) {
 throw new Exception('Unable to recognise filetype');
}

或者,您可以使用已弃用的 mime_content_type函数:

$filetype=mime_content_type($tmpFileName);

或使用操作系统的内置功能:

ob_start();
system('/usr/bin/file -i -b ' . realpath($tmpFileName));
$type = ob_get_clean();
$parts = explode(';', $type);
$filetype=trim($parts[0]);
于 2009-08-05T12:06:48.467 回答
2
function getMimeType( $filename ) {
        $realpath = realpath( $filename );
        if ( $realpath
                && function_exists( 'finfo_file' )
                && function_exists( 'finfo_open' )
                && defined( 'FILEINFO_MIME_TYPE' )
        ) {
                // Use the Fileinfo PECL extension (PHP 5.3+)
                return finfo_file( finfo_open( FILEINFO_MIME_TYPE ), $realpath );
        }
        if ( function_exists( 'mime_content_type' ) ) {
                // Deprecated in PHP 5.3
                return mime_content_type( $realpath );
        }
        return false;
}

这对我有用

为什么在 PHP 中不推荐使用 mime_content_type()?

于 2015-07-16T08:59:21.957 回答
1

我想我找到了一条捷径。使用以下方法获取图像大小:

$infFil=getimagesize($the_file_name);

Content-Type: <?php echo $infFil["mime"] ?>; name="<?php echo $the_file_name; ?>"

getimagesize返回具有 MIME 键的关联数组

我用过它并且有效

于 2015-11-21T22:15:10.890 回答
0

我已经尝试了大多数建议,但对我来说都失败了(我显然介于任何有用的 PHP 版本之间。我最终得到了以下功能:

function getShellFileMimetype($file) {
    $type = shell_exec('file -i -b '. escapeshellcmd( realpath($_SERVER['DOCUMENT_ROOT'].$file)) );
    if( strpos($type, ";")!==false ){
        $type = current(explode(";", $type));
    }
    return $type;
}
于 2017-05-01T10:39:45.203 回答
-3

尝试这个:

function ftype($f) {
                    curl_setopt_array(($c = @curl_init((!preg_match("/[a-z]+:\/{2}(?:www\.)?/i",$f) ? sprintf("%s://%s/%s", "http" , $_SERVER['HTTP_HOST'],$f) :  $f))), array(CURLOPT_RETURNTRANSFER => 1, CURLOPT_HEADER => 1));
                        return(preg_match("/Type:\s*(?<mime_type>[^\n]+)/i", @curl_exec($c), $m) && curl_getinfo($c, CURLINFO_HTTP_CODE) != 404)  ? ($m["mime_type"]) : 0;

         }
echo ftype("http://img2.orkut.com/images/medium/1283204135/604747203/ln.jpg"); // print image/jpeg
于 2010-09-08T04:27:14.077 回答
-3

我真的建议使用像“CodeIgniter”这样的框架来处理电子邮件。这是一个关于“在 18 分钟内使用 CodeIgniter 发送电子邮件”的截屏视频。

http://net.tutsplus.com/videos/screencasts/codeigniter-from-scratch-day-3/

于 2009-08-05T12:00:04.017 回答