php获取文件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版本限制的。

 代码如下 复制代码

<?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*/

时间: 2024-09-19 03:23:31

php获取文件mime类型程序代码的相关文章

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

 本文实例讲述了php准确获取文件MIME类型的方法.分享给大家供大家参考.具体实现方法如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

php准确获取文件MIME类型的方法_php技巧

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

php实现获取文件mime类型的方法_php技巧

本文实例讲述了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_t

php如何获取文件夹大小程序代码

 代码如下 复制代码 <?php   //获取文件夹大小 function dir_size($dir) {     if (!preg_match('#/$#', $dir)) {         $dir .= '/';     }     $totalsize = 0;     //调用文件列表     foreach (get_file_list($dir) as $name) {         $totalsize += (@is_dir($dir.$name) ? dir_size

PHP中获取文件扩展名程序代码

方法一,我自己最的一种获取扩展名方法  代码如下 复制代码 function extend_2($file_name) { $extend = pathinfo($file_name); $extend = strtolower($extend["extension"]); return $extend; } 方法二,利用explode函数与end函数组合操作  代码如下 复制代码 function get_extension($file) { return end(explode('

PHP获取文件绝对路径的代码(上一级目录)

PHP获取文件绝对路径 复制代码 代码如下: <?php echo __FILE__ ; // 取得当前文件的绝对地址,结果:D:\www\test.php echo dirname(__FILE__); // 取得当前文件所在的绝对目录,结果:D:\www\ echo dirname(dirname(__FILE__)); //取得当前文件的上一层目录名,结果:D:\ ?> chdir() 函数 定义和用法 chdir() 函数把当前的目录改变为指定的目录. 若成功,则该函数返回 true,

PHP获取文件绝对路径的代码(上一级目录)_php技巧

PHP获取文件绝对路径 复制代码 代码如下: <?php echo __FILE__ ; // 取得当前文件的绝对地址,结果:D:\www\test.php echo dirname(__FILE__); // 取得当前文件所在的绝对目录,结果:D:\www\ echo dirname(dirname(__FILE__)); //取得当前文件的上一层目录名,结果:D:\ ?> chdir() 函数 定义和用法 chdir() 函数把当前的目录改变为指定的目录. 若成功,则该函数返回 true,

smarty获取url参数传值程序代码

smarty获取url参数传值程序代码 <head> <meta http-equiv="content-type" content="text/html; charset=gb2312" /> <title>smarty获取url参数传值程序代码</title> </head> <body> <!--{$smarty.get.url}--> </body> </h

WordPress获取文章内容摘要程序代码

这个是我自己写的获取文章内容摘要程序  代码如下 复制代码 <?php  global $more;    $more = 1;    //1=全文 0=摘要   $my_content = strip_tags(get_the_excerpt(), $post->post_content); //获得文章   $my_content = str_replace(array("rn", "r", "n", " ",