php专区

 首页 > php专区 > PHP高级 > 文件上传 > php获取文件mime类型程序代码 - php文件操作

php获取文件mime类型程序代码 - php文件操作

分享到:
【字体:大 中 小】
导读:
          在php中获取文件的mime类型方法有很多种,我们来介绍直接利用mime_content_type()函数判断获取mime类型即可了 mime_content_type返回指定文...

php获取文件mime类型程序代码

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

mime_content_type返回指定文件的MIME类型,用法,代码如下:

  1. echo mime_content_type ( 'php.gif' ) . "n" ; 
  2. echo mime_content_type ( 'test.php' ); 

输出:image/gif,text/plain,但是此函数在php5.3.0以后就不可用了,如果你是php5.3.0以后版本我们可使用如下代码来操作:

  1. $finfo    = finfo_open(FILEINFO_MIME); 
  2. $mimetype = finfo_file($finfo, $filename); 
  3. finfo_close($finfo); 

下面介绍用户自定的我这个是没有php版本限制,代码如下:

  1. $mime = array ( 
  2.   //applications 
  3.   'ai'    => 'application/postscript', 
  4.   'eps'   => 'application/postscript', 
  5.   'exe'   => 'application/octet-stream', 
  6.   'doc'   => 'application/vnd.ms-word', 
  7.   'xls'   => 'application/vnd.ms-excel', 
  8.   'ppt'   => 'application/vnd.ms-powerpoint', 
  9.   'pps'   => 'application/vnd.ms-powerpoint', 
  10.   'pdf'   => 'application/pdf', 
  11.   'xml'   => 'application/xml', 
  12.   'odt'   => 'application/vnd.oasis.opendocument.text', 
  13.   'swf'   => 'application/x-shockwave-flash', 
  14.   // archives 
  15.   'gz'    => 'application/x-gzip', 
  16.   'tgz'   => 'application/x-gzip', 
  17.   'bz'    => 'application/x-bzip2', 
  18.   'bz2'   => 'application/x-bzip2', 
  19.   'tbz'   => 'application/x-bzip2', 
  20.   'zip'   => 'application/zip', 
  21.   'rar'   => 'application/x-rar', 
  22.   'tar'   => 'application/x-tar', 
  23.   '7z'    => 'application/x-7z-compressed', 
  24.   // texts 
  25.   'txt'   => 'text/plain', 
  26.   'php'   => 'text/x-php', 
  27.   'html'  => 'text/html', 
  28.   'htm'   => 'text/html', 
  29.   'js'    => 'text/javascript', 
  30.   'css'   => 'text/css', 
  31.   'rtf'   => 'text/rtf', 
  32.   'rtfd'  => 'text/rtfd', 
  33.   'py'    => 'text/x-python', 
  34.   'java'  => 'text/x-java-source', 
  35.   'rb'    => 'text/x-ruby', 
  36.   'sh'    => 'text/x-shellscript', 
  37.   'pl'    => 'text/x-perl', 
  38.   'sql'   => 'text/x-sql', 
  39.   // images 
  40.   'bmp'   => 'image/x-ms-bmp', 
  41.   'jpg'   => 'image/jpeg', 
  42.   'jpeg'  => 'image/jpeg', 
  43.   'gif'   => 'image/gif', 
  44.   'png'   => 'image/png', 
  45.   'tif'   => 'image/tiff', 
  46.   'tiff'  => 'image/tiff', 
  47.   'tga'   => 'image/x-targa', 
  48.   'psd'   => 'image/vnd.adobe.photoshop', 
  49.   //audio 
  50.   'mp3'   => 'audio/mpeg', 
  51.   'mid'   => 'audio/midi', 
  52.   'ogg'   => 'audio/ogg', 
  53.   'mp4a'  => 'audio/mp4', 
  54.   'wav'   => 'audio/wav', 
  55.   'wma'   => 'audio/x-ms-wma', 
  56.   // video 
  57.   'avi'   => 'video/x-msvideo', 
  58.   'dv'    => 'video/x-dv', 
  59.   'mp4'   => 'video/mp4', 
  60.   'mpeg'  => 'video/mpeg', 
  61.   'mpg'   => 'video/mpeg', 
  62.   'mov'   => 'video/quicktime', 
  63.   'wm'    => 'video/x-ms-wmv', 
  64.   'flv'   => 'video/x-flv', 
  65.   'mkv'   => 'video/x-matroska' 
  66.   ); 
  67. function _getMimeDetect() { 
  68.  if (class_exists('finfo')) { 
  69.   return 'finfo'; 
  70.  } else if (function_exists('mime_content_type')) { 
  71.   return 'mime_content_type'; 
  72.  } else if ( function_exists('exec')) { 
  73.   $result = exec('file -ib '.escapeshellarg(__FILE__)); 
  74.   if ( 0 === strpos($result, 'text/x-php') OR 0 === strpos($result, 'text/x-c++')) { 
  75.    return 'linux'; 
  76.   } 
  77.   $result = exec('file -Ib '.escapeshellarg(__FILE__)); 
  78.   if ( 0 === strpos($result, 'text/x-php') OR 0 === strpos($result, 'text/x-c++')) { 
  79.    return 'bsd'; 
  80.   } 
  81.  } 
  82.  return 'internal'; 
  83. } 
  84. function _getMimeType($path) { 
  85.  global $mime; 
  86.  $fmime = _getMimeDetect(); 
  87.  switch($fmime) { 
  88.   case 'finfo': 
  89.    $finfo = finfo_open(FILEINFO_MIME); 
  90.    if ($finfo)  
  91.     $type = @finfo_file($finfo, $path); 
  92.    break; 
  93.   case 'mime_content_type': 
  94.    $type = mime_content_type($path); 
  95.    break; 
  96.   case 'linux': 
  97.    $type = exec('file -ib '.escapeshellarg($path)); 
  98.    break; 
  99.   case 'bsd': 
  100.    $type = exec('file -Ib '.escapeshellarg($path)); 
  101.    break; 
  102.   default: 
  103.    $pinfo = pathinfo($path); 
  104.    $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : ''; 
  105.    $type = isset($mime[$ext]) ? $mime[$ext] : 'unkown'; 
  106.    break; 
  107.  } 
  108.  $type = explode(';', $type); 
  109.  
  110.  //需要加上这段,因为如果使用mime_content_type函数来获取一个不存在的$path时会返回'application/octet-stream' 
  111.  if ($fmime != 'internal' AND $type[0] == 'application/octet-stream') { 
  112.   $pinfo = pathinfo($path);  
  113.   $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : ''; 
  114.   if (!emptyempty($ext) AND !emptyempty($mime[$ext])) { 
  115.    $type[0] = $mime[$ext]; 
  116.   } 
  117.  } 
  118.  
  119.  return $type[0]; 
  120. } 
  121. $path = '1.txt';  //实际上当前路径并不存在1.txt 
  122. var_dump(_getMimeType($path)); 
  123. /*End of php*/
分享到:
php 创建文件多种方法总结 - php文件操作
php 创建文件多种方法总结 php创建文件的方法有很多种我们最常用的就是fopen,file_put_contents这两种方法来创建文件了,下面我来给大家详细介绍介绍,有需要了解的同学可参考。 创建php文件,代码如下:   例2,代码如下:   上面没作任何考虑,如果要全面点,我们首先确定...
php 文件目录操作函数 - php文件操作
php 文件目录操作函数 在php中我们有大量的可对文件与目录操作的函数,下面我来总结一下这些函数的用法. mkdir();函数:php创建文件夹和文件 ///创建文件夹   代码如下 复制代码  function createdir($dir)  {  if(file_exists($dir) && is_dir($dir)){//如果存在这个...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……