2012-03-25 5 views
0

codeIgniterのモデルからmime.typesファイルにアクセスしようとしています。codeIgniter - モデルからテキストファイルにアクセスする方法

function get_mime_type($filename, $mimePath = '../../assets'){ 
     $fileext = substr(strrchr($filename, '.'), 1); 
     if (empty($fileext)) return (false); 
     $regex = "/^([\w\+\-\.\/]+)\s+(\w+\s)*($fileext\s)/i"; 
     $lines = file("$mimePath/mime.types"); 
     foreach($lines as $line){ 
      if (substr($line, 0, 1) == '#') continue; // skip comments 
      $line = rtrim($line) . " "; 
      if (!preg_match($regex, $line, $matches)) continue; // no match to the extension 
       return ($matches[1]); 
     } 
     return (false); // no match at all 
    } 

私はそれを呼び出すとき、私はすでに、次のことを試してみた:

$this->get_mime_type($filename, $mimePath = '../../assets'); 

$this->get_mime_type($filename, $mimePath = '/zenoir/assets'); 

$this->get_mime_type($filename, $mimePath = 'http://localhost:8080/zenoir/assets/mime.types'); 

しかし、運。 mime.typesファイルには、資産フォルダにあり、モデルがinzenoir /アプリケーション/モデルである enter image description here

たエラー:

A PHPエラーが

重要度を発生しました:

警告メッセージ:ファイル(../../ assets/mime.types)[function.file]:ストリームを開くことに失敗しました:そのようなファイルまたはディレクトリなし

ファイル名:models/files.php

行番号:46

答えて

1

なぜあなたはMIMEタイプで非常に問題に陥っているのですか?代わりにmime_content_type()を使用してください。

echo mime_content_type($mimePath.$filename); 

そして、あなたのコードの問題はパスの問題です。代わりにBASEPATH定数を使用し、そこからトラバースしてください。あなたはこれらのすべての種類のパス問題を省略します。

+0

どのようにしてベースパスからトラバースできますか? – user225269

+0

@ user225269、CI 2の 'BASEPATH'リンクを'/system/'ディレクトリにリンクしている場合。あなたの場合の例として 'BASEPATH。../assets"を実行してください。 – Starx

関連する問題