Checking Mime-type for uploaded file in PHP - problem that making me confused so much

35 views
Skip to first unread message

Shaharia Azam

unread,
Oct 4, 2017, 10:08:16 AM10/4/17
to Google App Engine
Hello,
Today I am facing a very strange problem while I was just checking mime-type for uploaded files by my app users in PHP.

But it seems, none of the popular PHP functions are working.

Here it is I tried already.

// Solution # 1
$finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mtype = finfo_file($finfo, $tmpFile);
        finfo_close($finfo);
        if ($mtype == ("application/msword") ||
            $mtype == ("application/vnd.openxmlformats-officedocument.wordprocessingml.document") ||
            $mtype == ("application/pdf")
        ) {
            return true;
        } else {
            return false;
        }


//Solution # 2

$mimeArray = array(
            'application/msword',
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            'application/pdf'
        );
        $mime = mime_content_type($tmpFile);
        if (!in_array($mime, $mimeArray)) {
            return false;
        } else {
            return true;
        }



finfo_file, finfo_open, mime_content_type none of these functions are working. So for PHP application running on GAE standard environment, what's the most safest and easiest ways to detect file's mime-type in PHP?

Thanks in advance.


Regards,
Shaharia Azam

Yannick (Cloud Platform Support)

unread,
Oct 4, 2017, 10:44:48 AM10/4/17
to Google App Engine
The file info extension is available to be dynamically loaded onto App Engine. You should be able to use it once you add the following to your php.ini:

extension = "fileinfo.so"

Shaharia Azam

unread,
Oct 4, 2017, 12:13:27 PM10/4/17
to Google App Engine
Wow. awesome. It works. 
I guess, I should make a list of available dynamically loaded module list for GAE PHP Standard environment.

Btw, thanks.
Reply all
Reply to author
Forward
0 new messages