Get MIME Type via PHP

蓝咒 提交于 2019-11-30 19:42:12

问题


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?


回答1:


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.




回答2:


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).




回答3:


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.




回答4:


Parse your Apache's mime.types file.




回答5:


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



来源:https://stackoverflow.com/questions/3191579/get-mime-type-via-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!