Get MIME Type via PHP

2019-02-15 13:23发布

I have local access to files, which I need to get their MIME types. Working in WAMP/LAMP, CodeIgniter, and Zend libraries. What's the best way to get MIME type information?

5条回答
我想做一个坏孩纸
2楼-- · 2019-02-15 13:49

Parse your Apache's mime.types file.

查看更多
混吃等死
3楼-- · 2019-02-15 13:51

I think you need head. Quickest way is to do a head request, or in PHP under apache you can use apache_lookup_uri or in PHP 5.3 you can use FileInfo (I'd still recommend apache_lookup_uri or a simple head request though).

查看更多
唯我独甜
4楼-- · 2019-02-15 13:51

Try CI's built in function "get_mime_by_extension($file)". You can find it in the "system/helpers/file_helper.php" file

查看更多
放荡不羁爱自由
5楼-- · 2019-02-15 14:05

There's no easy way. You could try:
http://www.php.net/manual/en/function.finfo-file.php

// return mime type ala mimetype extension
$finfo = finfo_open(FILEINFO_MIME_TYPE);

Of course, this assumes you can install PECL extensions.

查看更多
Juvenile、少年°
6楼-- · 2019-02-15 14:08

Its never a good idea to try and find the mime type based on the file extension, as this can obviously be renamed by the used whos uploading - from .exe to .jpg

Real mime type detection is part of your overall security measures.

查看更多
登录 后发表回答