search
Homephp教程php手册php获取文件mime类型程序代码

在php中获取文件的mime类型方法有很多种,我们来介绍直接利用mime_content_type()函数判断获取mime类型即可了。

mime_content_type返回指定文件的MIME类型,

用法:

 代码如下 复制代码
echo mime_content_type ( 'php.gif' ) . "n" ;
echo mime_content_type ( 'test.php' );

输出:

image/gif
text/plain

但是此函数在php5.3.0以后就不可用了,如果你是php5.3.0以后版本我们可使用

 代码如下 复制代码

$finfo    = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);

来操作

下面介绍用户自定的我这个是没有php版本限制的。

 代码如下 复制代码

$mime = array (
  //applications
  'ai'    => 'application/postscript',
  'eps'   => 'application/postscript',
  'exe'   => 'application/octet-stream',
  'doc'   => 'application/vnd.ms-word',
  'xls'   => 'application/vnd.ms-excel',
  'ppt'   => 'application/vnd.ms-powerpoint',
  'pps'   => 'application/vnd.ms-powerpoint',
  'pdf'   => 'application/pdf',
  'xml'   => 'application/xml',
  'odt'   => 'application/vnd.oasis.opendocument.text',
  'swf'   => 'application/x-shockwave-flash',
  // archives
  'gz'    => 'application/x-gzip',
  'tgz'   => 'application/x-gzip',
  'bz'    => 'application/x-bzip2',
  'bz2'   => 'application/x-bzip2',
  'tbz'   => 'application/x-bzip2',
  'zip'   => 'application/zip',
  'rar'   => 'application/x-rar',
  'tar'   => 'application/x-tar',
  '7z'    => 'application/x-7z-compressed',
  // texts
  'txt'   => 'text/plain',
  'php'   => 'text/x-php',
  'html'  => 'text/html',
  'htm'   => 'text/html',
  'js'    => 'text/javascript',
  'css'   => 'text/css',
  'rtf'   => 'text/rtf',
  'rtfd'  => 'text/rtfd',
  'py'    => 'text/x-python',
  'java'  => 'text/x-java-source',
  'rb'    => 'text/x-ruby',
  'sh'    => 'text/x-shellscript',
  'pl'    => 'text/x-perl',
  'sql'   => 'text/x-sql',
  // images
  'bmp'   => 'image/x-ms-bmp',
  'jpg'   => 'image/jpeg',
  'jpeg'  => 'image/jpeg',
  'gif'   => 'image/gif',
  'png'   => 'image/png',
  'tif'   => 'image/tiff',
  'tiff'  => 'image/tiff',
  'tga'   => 'image/x-targa',
  'psd'   => 'image/vnd.adobe.photoshop',
  //audio
  'mp3'   => 'audio/mpeg',
  'mid'   => 'audio/midi',
  'ogg'   => 'audio/ogg',
  'mp4a'  => 'audio/mp4',
  'wav'   => 'audio/wav',
  'wma'   => 'audio/x-ms-wma',
  // video
  'avi'   => 'video/x-msvideo',
  'dv'    => 'video/x-dv',
  'mp4'   => 'video/mp4',
  'mpeg'  => 'video/mpeg',
  'mpg'   => 'video/mpeg',
  'mov'   => 'video/quicktime',
  'wm'    => 'video/x-ms-wmv',
  'flv'   => 'video/x-flv',
  'mkv'   => 'video/x-matroska'
  );

function _getMimeDetect() {
 if (class_exists('finfo')) {
  return 'finfo';
 } else if (function_exists('mime_content_type')) {
  return 'mime_content_type';
 } else if ( function_exists('exec')) {
  $result = exec('file -ib '.escapeshellarg(__FILE__));
  if ( 0 === strpos($result, 'text/x-php') OR 0 === strpos($result, 'text/x-c++')) {
   return 'linux';
  }
  $result = exec('file -Ib '.escapeshellarg(__FILE__));
  if ( 0 === strpos($result, 'text/x-php') OR 0 === strpos($result, 'text/x-c++')) {
   return 'bsd';
  }
 }
 return 'internal';
}

function _getMimeType($path) {
 global $mime;
 $fmime = _getMimeDetect();
 switch($fmime) {
  case 'finfo':
   $finfo = finfo_open(FILEINFO_MIME);
   if ($finfo)
    $type = @finfo_file($finfo, $path);
   break;
  case 'mime_content_type':
   $type = mime_content_type($path);
   break;
  case 'linux':
   $type = exec('file -ib '.escapeshellarg($path));
   break;
  case 'bsd':
   $type = exec('file -Ib '.escapeshellarg($path));
   break;
  default:
   $pinfo = pathinfo($path);
   $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
   $type = isset($mime[$ext]) ? $mime[$ext] : 'unkown';
   break;
 }
 $type = explode(';', $type);
 
 //需要加上这段,因为如果使用mime_content_type函数来获取一个不存在的$path时会返回'application/octet-stream'
 if ($fmime != 'internal' AND $type[0] == 'application/octet-stream') {
  $pinfo = pathinfo($path);
  $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
  if (!empty($ext) AND !empty($mime[$ext])) {
   $type[0] = $mime[$ext];
  }
 }
 
 return $type[0];
}

$path = '1.txt';  //实际上当前路径并不存在1.txt
var_dump(_getMimeType($path));

/*End of php*/



文章链接:

随便收藏,请保留本文地址!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),