简体   繁体   中英

How to check mime type of file uploaded image

I'm trying to check the mime type of an image. This is my code:

    private function checkMIMEType() {
        $finfo = new finfo(FILEINFO_MIME_TYPE);
        $this->_dataArray['ext'] = array_search(
            $finfo->file($this->_dataArray['tmp_name']),
            $this->_mimeArray,
            true
        );
        if($this->_dataArray['ext']===false) {
            $this->error = "File not supported";
            return false;
        } else {
            return true;
        }
    }

This is the error: Warning: require_once(classes/finfo.php): failed to open stream: No such file or directory in D:\\XAMPP\\htdocs

What I've got from now on is from this site: http://php.net/manual/en/class.finfo.php

And I think It's because I have a wrong version of the PHP because I wrote this online works with php > 5.3.0

Is there any way this code can run with a later version? Or is there something wrong with my code?

You're right on the money- this was introduced in PHP 5.3.0, so any older version will not have this class defined.

However, this class was a package before it was incorporated into PHP. You can get it from here: https://pecl.php.net/package/Fileinfo

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