2008年

PHP の mime_content_type 関数がなかった このエントリーを含むはてなブックマーク

PHP Add comments

今携わっている案件で、PHP の mime_content_type 関数が使用されていた。しかしローカル開発環境の Mac に MacPort で入れた PHP には入っていなかった。 variants も調べてみたがなさそうだ。PHP マニュアルには mime_content_type 関数は非推奨で、PECL の Fileinfo を使用するように書かれている。Fileinfo も組み込むのが時間的に面倒だったので、file コマンドを使用してグローバル関数として mime_content_type 関数を自作してみた。

if ( !function_exists('mime_content_type') ) {
    function mime_content_type($filename) {
        $mime_type = exec('file -Ib '.$filename);
        return $mime_type;
    }
}

これを Subversion 管理以外のローカル設定ファイルに書いておけば、ローカル開発環境では自作の関数が使用され、テスト環境、本番環境では PHP 組み込みの mime_content_type 関数が使用される。

関連する投稿

One Response to “PHP の mime_content_type 関数がなかった”

  1. uribo Says:

    報告です。3行目ですが、

    $mime_type = exec(‘file -Ib ‘.$filename);

    ではなく

    $mime_type = exec(‘file -ib ‘.$filename);

    だと上手く動きました。

Leave a Reply

Additional comments powered by BackType

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS ログイン