Php codeIgniter-如何从模型访问文本文件
我正在尝试从codeIgniter中的模型访问mime.types文件Php codeIgniter-如何从模型访问文本文件,php,codeigniter,Php,Codeigniter,我正在尝试从codeIgniter中的模型访问mime.types文件 function get_mime_type($filename, $mimePath = '../../assets'){ $fileext = substr(strrchr($filename, '.'), 1); if (empty($fileext)) return (false); $regex = "/^([\w\+\-\.\/]+)\s+(\w+\s)*($
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文件位于assets文件夹中,模型为inzenoir/application/models
错误是:
遇到一个PHP错误
严重性:警告
消息:文件(../../assets/mime.types)[function.file]:无法打开流:没有这样的文件或目录
文件名:models/files.php
行号:46为什么mime类型会给您带来这么多麻烦,请改用
代码的问题是路径问题。使用
BASEPATH
常量,然后从那里开始遍历。您将忽略所有这些类型的路径问题。如何从基本路径进行遍历?@user225269,如果您使用的是指向/system/
目录的CI 2基本路径
链接。只需执行BASEPATH。“../assets”
作为您案例的示例。
echo mime_content_type($mimePath.$filename);