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関数を使用しようとします。それらが利用できない場合は、代替を使用し、 Upgrade.phpライブラリmime_content_typeからのドロップイン置換を含めて、これが存在することを確認します。それらが有用なものを返さなかった場合、OS のコマンドを試します。*NIXシステムでのみ利用可能なAFAIK。これをWindowsで使用する予定がある場合は、これを変更するか、削除することをお勧めします。何も機能しない場合は、画像のみのフォールバックとして試行されます。fileexif_imagetype

私は、サーバーによって MIME タイプ関数のサポートが大きく異なり、Upgrade.php のmime_content_type置き換えが完全ではないことに気付きました。exif_imagetypeただし、元の機能と Upgrade.php の置き換えの両方の限定された機能は、かなり確実に機能しています。画像だけに関心がある場合は、この最後の画像のみを使用することをお勧めします。

于 2010-09-08T04:35:43.027 に答える
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);

または、OS の組み込み関数を使用します。

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

getimagesizeMIME キーを持つ連想配列を返します

私はそれを使用し、それは動作します

于 2015-11-21T22:15:10.890 に答える
-3

メールの送信には「CodeIgniter」のようなフレームワークを使用することを強くお勧めします。これは、「CodeIgniter を使用してメールを送信する」に関する 18 分間のスクリーンキャストです。

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

于 2009-08-05T12:00:04.017 に答える
-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 に答える