简体   繁体   English

通过PHP获取MIME类型

[英]Get MIME Type via PHP

I have local access to files, which I need to get their MIME types. 我可以本地访问文件,我需要获取它们的MIME类型。 Working in WAMP/LAMP, CodeIgniter, and Zend libraries. 在WAMP / LAMP,CodeIgniter和Zend库中工作。 What's the best way to get MIME type information? 获取MIME类型信息的最佳方法是什么?

There's no easy way. 没有简单的方法。 You could try: 您可以尝试:
http://www.php.net/manual/en/function.finfo-file.php 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. 当然,这假设您可以安装PECL扩展。

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). 最快的方法是发出头请求,或者在apache下的PHP中可以使用apache_lookup_uri或在PHP 5.3中可以使用FileInfo(尽管我仍然建议apache_lookup_uri或一个简单的头请求)。

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 尝试根据文件扩展名查找mime类型绝不是一个好主意,因为显然可以由使用过的Whos上传来重命名-从.exe到.jpg

Real mime type detection is part of your overall security measures. 真实的mime类型检测是整体安全措施的一部分。

解析您的Apache的mime.types文件。

Try CI's built in function "get_mime_by_extension($file)". 尝试使用CI的内置函数“ get_mime_by_extension($ file)”。 You can find it in the "system/helpers/file_helper.php" file 您可以在“ system / helpers / file_helper.php”文件中找到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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