2012-03-25 46 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

是錯誤:

一個PHP錯誤遇到

嚴重性:警告

留言:文件(../../ assets/mime.types)[function.file]:無法打開流:沒有這樣的文件或目錄

文件名:models/files.php

行號:46

回答

1

爲什麼你會爲MIME類型陷入如此多的麻煩,請使用mime_content_type()來代替。

echo mime_content_type($mimePath.$filename); 

而你的代碼的問題是路徑問題。使用BASEPATH常量並從那裏遍歷。您將省略所有這些路徑問題。

+0

我該如何從基路徑中遍歷? – user225269 2012-03-25 06:13:19

+0

@ user225269,如果您使用CI 2'BASEPATH'鏈接到'/ system /'目錄。只要做'BASEPATH。'../ assets''作爲你案例的一個例子。 – Starx 2012-03-25 06:16:38