欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php準確獲取文件MIME類型的方法

 更新時間:2015年06月17日 15:44:44   作者:邪惡的小Y  
這篇文章主要介紹了php準確獲取文件MIME類型的方法,涉及php針對文件屬性操作的相關(guān)技巧,需要的朋友可以參考下

本文實例講述了php準確獲取文件MIME類型的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

<?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函數(shù)來獲取一個不存在的$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'; //實際上當(dāng)前路徑并不存在1.txt
var_dump(_getMimeType($path));
/*End of php*/

希望本文所述對大家的php程序設(shè)計有所幫助。

  • php ajax異步讀取rss文檔數(shù)據(jù)

    php ajax異步讀取rss文檔數(shù)據(jù)

    這篇文章主要介紹了php ajax異步讀取rss文檔數(shù)據(jù)的相關(guān)資料,RSS是一種描述和同步網(wǎng)站內(nèi)容的格式,需要的朋友可以參考下
    2016-03-03
  • php引用計數(shù)器進行垃圾收集機制介紹

    php引用計數(shù)器進行垃圾收集機制介紹

    每一種計算機語言都有自己的自動垃圾回收機制,讓程序員不必過分關(guān)心程序內(nèi)存分配,php也不例外,但是在面向?qū)ο缶幊?OOP)編程中,有些對象需要顯式的銷毀;防止程序執(zhí)行內(nèi)存溢出
    2012-09-09
  • php實現(xiàn)的二分查找算法示例

    php實現(xiàn)的二分查找算法示例

    這篇文章主要介紹了php實現(xiàn)的二分查找算法,結(jié)合具體實例形式分析了php二分查找算法的實現(xiàn)與使用技巧,涉及php數(shù)組判斷、遍歷、計算等相關(guān)操作,需要的朋友可以參考下
    2017-06-06
  • PHP常用開發(fā)函數(shù)解析之?dāng)?shù)組篇[未完結(jié)]

    PHP常用開發(fā)函數(shù)解析之?dāng)?shù)組篇[未完結(jié)]

    數(shù)組處理函數(shù)在PHP開發(fā)中非常常見,學(xué)習(xí)好數(shù)組處理函數(shù)至關(guān)重要.數(shù)組處理函數(shù)在實際應(yīng)用中涉及到:數(shù)組的創(chuàng)建,字符串于數(shù)組的相互轉(zhuǎn)換,數(shù)組轉(zhuǎn)XML,數(shù)組轉(zhuǎn)JSON.數(shù)組的檢測.數(shù)組的合并于分割.數(shù)組的數(shù)目.獲取數(shù)組中的所有值,獲取數(shù)組中的所有鍵值
    2012-07-07
  • 一個數(shù)據(jù)采集類

    一個數(shù)據(jù)采集類

    一個數(shù)據(jù)采集類...
    2007-02-02
  • PHP實現(xiàn)在windows下配置sendmail并通過mail()函數(shù)發(fā)送郵件的方法

    PHP實現(xiàn)在windows下配置sendmail并通過mail()函數(shù)發(fā)送郵件的方法

    這篇文章主要介紹了PHP實現(xiàn)在windows下配置sendmail并通過mail()函數(shù)發(fā)送郵件的方法,較為詳細的分析了Windows平臺sendmail配置步驟與相應(yīng)的使用技巧,需要的朋友可以參考下
    2017-06-06
  • php 無限極分類

    php 無限極分類

    至于添加刪除之類的功能我就不多寫了!仔細看看就知道這么用了. 難的是顯示方面 希望高手擴展一下! 這是類
    2008-03-03
  • php實現(xiàn)收發(fā)郵件的方法詳解

    php實現(xiàn)收發(fā)郵件的方法詳解

    這篇文章主要為大家詳細介紹了php實現(xiàn)收發(fā)郵件的多種方法總結(jié),文中的示例代碼講解詳細,具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以跟隨小編一起了解下
    2023-11-11
  • 最新評論