简体   繁体   中英

codeIgniter -How to access a text file from a model

I'm trying to access the mime.types file from a model in 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 
    } 

I've already tried the following when I call it:

$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');

But no luck. The mime.types file is in the assets folder and the model is inzenoir/application/models 在此处输入图片说明

The error was:

A PHP Error was encountered

Severity: Warning

Message: file(../../assets/mime.types) [function.file]: failed to open stream: No such file or directory

Filename: models/files.php

Line Number: 46

Why are you getting into so much trouble for the mime types, use mime_content_type() instead.

echo mime_content_type($mimePath.$filename);

And the problem with your code is the path issue. Use BASEPATH constant and traverse from there instead. You will omit all these sort of path issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM