欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-06 11:46:18
本文实例讲述了php获取文件mime类型的方法。分享给大家供大家参考。具体如下:1.使用 mime_content_type 方法string mime_content_type ( string $...

本文实例讲述了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文件类型不一定是非常准确的。

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