2

通常の txt ドキュメントをアップロードしようとすると、「ファイル タイプが許可されていません」というエラーが表示されることに気付きました。次のコマンドを使用して MIME タイプを確認しました。

~ $ file --mime /home/user/Documents/New_Linux_Installation.txt
/home/user/Documents/New_Linux_Installation.txt: text/plain; charset=utf-8

しかし、codeigniter のアップロード クラスは、text/x-lisp という MIME タイプを提供します。なぜそれが考えられるのですか?PHP 5.3.8 を使用しています。MIME タイプを生成するコードの一部を次に示します。MIME タイプを表示するために log_message 部分を追加しました。

protected function _file_mime_type($file)
    {
        // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
        $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';

        /* Fileinfo extension - most reliable method
         *
         * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the
         * more convenient FILEINFO_MIME_TYPE flag doesn't exist.
         */
        if (function_exists('finfo_file'))
        {
            $finfo = finfo_open(FILEINFO_MIME);

            if (is_resource($finfo)) // It is possible that a FALSE value is returned, if there is no magic MIME database file found on the system
            {
                $mime = @finfo_file($finfo, $file['tmp_name']);
                finfo_close($finfo);

                log_message('error', 'mime: '.var_export($mime, TRUE), '', 'debug');

                /* According to the comments section of the PHP manual page,
                 * it is possible that this function returns an empty string
                 * for some files (e.g. if they don't exist in the magic MIME database)
                 */
                if (is_string($mime) && preg_match($regexp, $mime, $matches))
                {
                    $this->file_type = $matches[1];
                    return;
                }
            }
        }

興味深いのは、元のファイルのアップロード情報が正しい MIME タイプを示していることです。$_FILES['filename']['type'] 変数について具体的に話しています。それを間違ったものに変更するのは、codeigniter の処理中です。

4

1 に答える 1