PHP实现获取文件mime类型多种方法解析

本文实例讲述了php获取文件mime类型的方法。分享给大家供大家参考。具体如下:

1.使用 mime_content_type 方法

string mime_content_type ( string $filename )

Returns the MIME content type for a file as determined by using information from the magic.mime file.

<?php
$mime_type = mime_content_type('1.jpg');
echo $mime_type; // image/jpeg
?>

但此方法在 php5.3 以上就被废弃了,官方建议使用 fileinfo 方法代替。

2.使用 Fileinfo 方法 (官方推荐)

使用fileinfo需要安装php_fileinfo扩展。

如已安装可以在extension_dir目录下找到php_fileinfo.dll(windows),fileinfo.so(linux)

打开php.ini,把extension=php_fileinfo.dll前的";"去掉,然后重启apache。

<?php
$fi = new finfo(FILEINFO_MIME_TYPE);
$mime_type = $fi->file('1.jpg');
echo $mime_type; // image/jpeg
?>

3.使用 image_type_to_mime_type 方法(只能处理图象类型)

使用exif_imagetype方法需要安装php_exif扩展,并需要安装php_mbstring扩展

如已安装可以在extension_dir目录下找到php_exif.dll(windows),exif.so(linux)

打开php.ini,把 extension=php_mbstring.dll, extension=php_exif.dll 前的","去掉,然后重启apache

<?php
$image = exif_imagetype('1.jpg');
$mime_type = image_type_to_mime_type($image);
echo $mime_type; // image/jpeg
?>

Tips:如果使用文件名的后缀来判断,因为文件后缀是可以修改的,所以使用文件后缀来判断会不准确。

php获取文件mime类型Fileinfo等方法

前几天写到使用wordpress xmlrpc api远程发布文章,如果本地服务器的文章库里某一篇待发表的wordpress文章包含图片文件时,就会使用到WordPress上传文件的API metaWeblog.newMediaObject,该api需要提供文件的mime 类型。php如 何获取文件(图片)的mime 类型呢?最初远方博客使用php mime_content_type()函数,使用开发用的ubuntu server lamp的默认配置测试后完全支持,返回了正确的文件mime type。但是将该API项目移植到Centos 5.2(内核2.6) LAMP环境时,出现了如下错误提示:

Fatal error: Call to undefined function: mime_content_type()

最后查看了最新的php手册发现php mime_content_type()函数已经被废弃,当然官方不推荐使用,而且需要经过适当的php配置后才能使用。因此要获取图片或其他的文件的 MIME类型,Fatal error: Call to undefined function: mime_content_type()错误就有了以下几种解决方案。

mime_content_type()函数判断获取mime类型

如果对已被php 5.3.0废弃的mime_content_type()函数仍然情有独钟,那么可以对php进行配置启用magic_mime扩展。比如Centos下 使用phpinfo()查看php apache配置,查找到mime-magic,如果显示“--without-mime-magic”,则要编译php切换到”with-mime- magic“选 项。mime_content_type()函数还依赖于Apache httpd 的magic文件(mime_magic.magicfile),为了检测文件的MIME类型,必须配置告知magic文件的地址,如'–with- mime-magic=/usr/share/file/magic.mime'。Windows环境下还需要在php.ini中添加:

mime_magic.magicfile = "$PHP_INSTALL_DIRmagic.mime"

其中$PHP_INSTALL_DIR是你的php安装目录。在有些LAMP环境下,这个mime_magic文件不一定存在或可读,还要另外下载。另外 有些虚拟主机为了安全考虑,即使是有with-mime-magic也不一定会返回正确的mime类型,有时候会返回空字符串。因此,就凭 mime_content_type()函数已经被废弃这一项,就不推荐使用该方法获取文件MIME类型了。

php Fileinfo 获取文件MIME类型(finfo_open)

PHP官方推荐mime_content_type()的替代函数是Fileinfo函数。PHP 5.3.0+已经默认支持Fileinfo函数(fileinfo support-enabled),不必进行任何配置即可使用finfo_open()判断获取文件MIME类型。Centos 默认安装的LAMP环境php版本还是PHP5.2.6,低于5.3.0版本则可能出现类似错误提示:PHP Fatal error: Call to undefined function finfo_open() in…。因为之前的php版本,需要加载magic_open类,fileinfo函数属于PECL扩展,启用fileinfo PECL扩展才能检测MIME类型。所以有两种途径使用fileinfo获取文件的MIME类型。

将php版本升级到5.3.0以上。php官方也已经不再维护和更新这个fileinfo pecl扩展包,所以升级是最好的办法。

安装fileinfo pecl扩展,Centos linux 如何安装fileinfo:在Centos下面安装fileinfo命令(rpm):yum install php-pecl-Fileinfo。或使用源码安装编译:

cd /usr/src/down && wget http://pecl.php.net/get/Fileinfo-1.0.4.tgz
tar zxvf Fileinfo-1.0.4.tgz
cd /usr/src/down/Fileinfo-1.0.4 && phpize && ./configure && make && make install

还可以使用网上流传较多的一种方法,Linux通过phpize使用pecl指令来安装fileinfo:

  • 若没有phpize指令,需要先安装。#phpize检测若提示”No command ‘phpize' found”,则需先安装phpize;
  • 下载安装php-devel(php5-dev)的rpm,安装phpize;
  • service httpd restart 或 reboot;
  • 命令 pecl install fileinfo 安装fileinfo扩展。
  • 安装完毕,/usr/lib/php/module目录下多了fileinfo.so文件,/usr/share/file目录下多了magic.mime和magic两个文档
  • 修改php.ini配置:加入 extension=”fileinfo.so”
  • service httpd restart
  • Windows服务器下安装fileinfo相似,php.ini:extension=php_fileinfo.dll

image_type_to_mime_type()获取图片MIME类型

如果我们需要判断MIME类型的文件只有图像文件,那么首先可以使用exif_imagetype()函数获取图像类型常量,再用 image_type_to_mime_type()函数将图像类型常量转换成图片文件的MIME类型。同样php.ini中要配置打开 php_mbstring.dll(Windows需要)和extension=php_exif.dll。phpinfo()“–enable-exif”。首先exif_imagetype返回的是图像类型常量(Imagetype Constants),如IMAGETYPE_GIF、IMAGETYPE_JPEG、IMAGETYPE_PNG等。

<?php
$image = exif_imagetype("D:farleeinfo.jpg");  //本地路径或远程图片地址均可 IMAGETYPE_GIF//
$image = exif_imagetype("http://farlee.info/wp-content/images/rss_feedsky.gif");
$mime = image_type_to_mime_type($image);
echo $mime; // 输出image/jpeg
?>

php上传文件获取MIME类型

如果使用php上传文件,检测上传文件的MIME类型,则可以使用全局变量$_FILES['uploadfile']['type'],由客户端的浏览器检测获取文件MIME类型。

Centos 系统或其他环境下若都不方便获取文件MIME类型的话,还有最后一种绝对可行的方法,就是读取文件名后缀,根据后缀名一一对应文件的MIME类型,具体可以参考php手册上的这条评论。当然这种方法检测到的MIME文件类型不一定是非常准确的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • PHP实现的获取文件mimes类型工具类示例

    本文实例讲述了PHP实现的获取文件mimes类型工具类.分享给大家供大家参考,具体如下: <?php /* * Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in com

  • PHP实现获取url地址中顶级域名的方法示例

    本文实例讲述了PHP实现获取url地址中顶级域名的方法.分享给大家供大家参考,具体如下: parse_url()获取到的host时多级域名,如:mp.weixin.qq.com.做域名黑名单的时候我们需要得到顶级域名. 有不足之处还请留言指正,谢谢. <?php /** * @Author: Ding Jianlong * @Date: 2019-03-07 16:14:04 * @Last Modified by: Ding Jianlong * @Last Modified time: 20

  • PHP 利用Mail_MimeDecode类提取邮件信息示例

    重点为one_mail函数.利用Mail_mimeDecode类从邮件中提取邮件头和邮件正文. 复制代码 代码如下: <?php header("content-type:text/html; charset=UTF-8"); /* * record kid words and insert into database * user by sending email to publication kid words * */ include 'POP3.php'; include

  • php实现获取文件mime类型的方法

    本文实例讲述了php获取文件mime类型的方法.分享给大家供大家参考.具体如下: 1.使用 mime_content_type 方法 string mime_content_type ( string $filename ) Returns the MIME content type for a file as determined by using information from the magic.mime file. <?php $mime_type = mime_content_typ

  • php 比较获取两个数组相同和不同元素的例子(交集和差集)

    1.获取数组相同元素 array_intersect()该函数比较两个(或更多个)数组的键值,并返回交集数组,该数组包括了所有在被比较的数组(array1)中, 同时也在任何其他参数数组(array2 或 array3 等等)中的键值. <?php $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"

  • php准确获取文件MIME类型的方法

    本文实例讲述了php准确获取文件MIME类型的方法.分享给大家供大家参考.具体实现方法如下: <?php $mime = array ( //applications 'ai' => 'application/postscript', 'eps' => 'application/postscript', 'exe' => 'application/octet-stream', 'doc' => 'application/vnd.ms-word', 'xls' => '

  • PHP的反射动态获取类方法、属性、参数操作示例

    本文实例讲述了PHP的反射动态获取类方法.属性.参数操作.分享给大家供大家参考,具体如下: 我们可以在PHP运行时,通过PHP的反射动态的获取类的方法.属性.参数等详细信息. 用途:插件的设计,文档的自动生成,扩充PHP语言. <?php class Person { const weightUnit = 'kg'; const heightUnit = 'cm'; public $name = 'test'; public $age = 1; public function say($msg

  • php文件类型MIME对照表(比较全)

    由于内容过多,大家可以通过ctrl+F搜索即可 IE浏览器 id 后缀名 php识别出的文件类型 0 gif image/gif 1 jpg image/jpeg 2 png image/png 3 bmp image/bmp 4 psd application/octet-stream 5 ico image/x-icon 6 rar application/octet-stream 7 zip application/zip 8 7z application/octet-stream 9 e

  • PHP实现获取文件mime类型多种方法解析

    本文实例讲述了php获取文件mime类型的方法.分享给大家供大家参考.具体如下: 1.使用 mime_content_type 方法 string mime_content_type ( string $filename ) Returns the MIME content type for a file as determined by using information from the magic.mime file. <?php $mime_type = mime_content_typ

  • python 检查文件mime类型的方法

    magic 模块可以检查文件的mime类型,而不是从后缀名来判断,例如判断文件是不是视频或图片类型如下: #检查文件类型 mime_type = magic.from_file(full_path,mime=True) logger.info("上传的文件类型:"+str(mime_type)) if not mime_type.startswith('video') and not mime_type.startswith('image'): logger.error("非

  • SWFUpload与CI不能正确上传识别文件MIME类型解决方法分享

    解决方案如下,其它框架雷同. 源代码(/system/libraries/upload.php 199 line) $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']); 修改成如下: 复制代码 代码如下: //Edit By Tacker if(function_exists('mime_content_type')){ $this->file_t

  • Java获取文件的类型和扩展名的实现方法

    Java获取文件的类型和扩展名 实现代码: File file=new File("E:\\aa.jpg"); String fileName=file.getName(); String fileTyle=fileName.substring(fileName.lastIndexOf("."),fileName.length()); System.out.println(fileTyle); 程序运行效果图: 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

  • JavaScript 检测文件的类型的方法

    我们会想到通过 input 元素的 accept 属性来限制上传的文件类型: <input type="file" id="inputFile" accept="image/png" /> 这种方案虽然可以满足大多数场景,但如果用户把 JPEG 格式的图片后缀名更改为 .png 的话,就可以成功突破这个限制.那么应该如何解决这个问题呢?其实我们可以通过读取文件的二进制数据来识别正确的文件类型.在介绍具体的实现方案前,阿宝哥先以图片类型

  • java获取文件扩展名的方法小结【正则与字符串截取】

    本文实例讲述了java获取文件扩展名的方法.分享给大家供大家参考,具体如下: 问题描述:  有一个String类型:String imageName = "zy.jpg"; 请问我如何截取"."后面的后辍名. 解决方法一:使用正则表达式 package csdnTest; import java.util.regex.*; public class CSDNTest { public static void main(String[] ss) { String s=

  • VB.NET获取文件默认图标的方法

    本文实例讲述了VB.NET获取文件默认图标的方法.分享给大家供大家参考.具体如下: 该段代码帮助你获取计算机上的任何文件的默认图标,使用Shell32.dll. Private Structure SHFILEINFO Public hIcon As IntPtr Public iIcon As Integer Public dwAttributes As Integer <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _ Public

随机推荐