mime_content_type() - fileinfo函数(文件信息)
mime_content_type()
(PHP 4 >= 4.3.0, PHP 5, PHP 7)
检测文件的 MIME 类型
说明
mime_content_type(string $filename): string返回通过使用magic.mime检测到的文件 MIME 类型。
参数
$filename要检测的文件名。
返回值
返回文件的 MIME 内容类型,例如text/plain或application/octet-stream。或者在失败时返回FALSE
。
错误/异常
失败时抛出E_WARNING
警告。
范例
mime_content_type()示例
以上例程会输出:
image/gif text/plain
参见
finfo_file()
别名 finfo_file()finfo_buffer()
别名 finfo_buffer()
Fast generation of uptodate mime types: Output: $mime_types = array( '123' => 'application/vnd.lotus-1-2-3', '3dml' => 'text/vnd.in3d.3dml', '3g2' => 'video/3gpp2', '3gp' => 'video/3gpp', '7z' => 'application/x-7z-compressed', 'aab' => 'application/x-authorware-bin', 'aac' => 'audio/x-aac', 'aam' => 'application/x-authorware-map', 'aas' => 'application/x-authorware-seg', ... Enjoy.
Completing comment: 0 stringThere is a composer package that will do this: https://github.com/ralouphie/mimey to output a human-readable string, like "HTML document text", which can sometimes be useful. The only drawback is that your scripts will not work on Windows, but is this such a problem? Just about all web hosts use a UNIX. It is a far better way than just examining the file extension.using should work on most shared linux hosts without errors. It should also work on Windows hosts with msysgit installed.Lukas V is IMO missing some point. The MIME type of a file may not be corresponding to the file suffix. Imagine someone would obfuscate some PHP code in a .gif file, the file suffix would be 'GIF' but the MIME would be text/plain or even text/html. Another example is files fetched via a distant server (wget / fopen / file / fsockopen...). The server can issue an error, i.e. 404 Not Found, wich again is text/html, whatever you save the file to (download_archive.rar). His provided function should begin by the test of the function existancy like : function MIMEalternative($file) { if(function_exists('mime_content_type')) return mime_content_type($file); else return ($file); }if you use a transparent 'spacer' GIF i've found it needs to be a around 25x25 for it to register as 'image/gif'. otherwise it's read in as 'text/plain'.For me mime_content_type didn't work in Linux before I added mime_magic.magicfile = "/usr/share/magic.mime" to php.ini (remember to find the correct path to mime.magic)The correct little correction: exec will return the mime with a newline at the end, the trim() should be called with the result of exec, not the other way around.Here's a simple function to return MIME types, based on the Apache mime.types file. [The one in my previous submission, which has since been replaced by this one] only works properly if mime.types is formatted as Windows text. The updated version below corrects this problem. Thanks to Mike for pointing this out. Notes: [1] Requires mime.types file distributed with Apache (normally found at ServerRoot/conf/mime.types). If you are using shared hosting, download the file with the Apache distro and then upload it to a directory on your web server that php has access to. [2] First param is the filename (required). Second parameter is path to mime.types file (optional; defaults to home/etc/). [3] Based on MIME types registered with IANA (http://www.iana.org/assignments/media-types/index.html). Recognizes 630 extensions associated with 498 MIME types. [4] Asserts MIME type based on filename extension. Does not examine the actual file; the file does not even have to exist. [5] Examples of use: >> echo get_mime_type('myFile.xml'); >> application/xml >> echo get_mime_type('myPath/myFile.js'); >> application/javascript >> echo get_mime_type('myPresentation.ppt'); >> application/vnd.ms-powerpoint >> echo get_mime_type('http://mySite.com/myPage.php); >> application/x-httpd-php >> echo get_mime_type('myPicture.jpg'); >> image/jpeg >> echo get_mime_type('myMusic.mp3'); >> audio/mpeg and so on... To create an associative array containing MIME types, use:I also had issues with this function. The issue was that it would almost always return "text/plain". echo ini_get('mime_magic.magicfile'); // returns /etc/httpd/conf/magic I found that I needed the OS' magic.mime file instead. You can either copy it to the existing location, or update your php.ini, you cannot use ini_set(). [root@blade conf]# mv magic magic.old [root@blade conf]# cp /usr/share/magic.mime magic [root@blade conf]# apachectl graceful Note: you will see that I have gracefully restarted apache to ensure it has taken affect.Not perfect, but works good enough for me ;)I added these two lines to my magic.mime file: 0 string \':false; } return $returned; } $file_name = 'mime-array.php'; $data = generateUpToDateMimeArray(APACHE_MIME_TYPES_URL); if ($data != "ERROR") { $file = fopen($file_name, 'wb') or die("cannot open $file_name\n"); fwrite($file, $data ) or die("cannot write data\n"); fclose($file); echo "updated"; } else { echo "faliure"; } ?> All you need to do is run it once and in your code you can simply add these two lines: enjoyadditional to svogal's video files: 'mp4' => 'video/mp4', what also works: 'mp4' => 'audio/mp4',Since I enabled the mime_magic extension on my IIS, I also got the error message "invalid magic file, disabled" in my phpinfo. After I add these lines to my php.ini, the message disappeared and it works great! mime_magic.debug = Off mime_magic.magicfile ="D:\PHP5\extras\magic.mime" mime_magic.debug is by default off but without this line it fails. I'm using PHP 5.2.5.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)