PHP 是不是具有在给定有效内容类型的情况下返回正确文件扩展名的函数?
Posted
技术标签:
【中文标题】PHP 是不是具有在给定有效内容类型的情况下返回正确文件扩展名的函数?【英文标题】:Does PHP have a function that returns the correct file extension given a valid content type?PHP 是否具有在给定有效内容类型的情况下返回正确文件扩展名的函数? 【发布时间】:2012-02-16 13:05:41 【问题描述】:php 是否具有返回给定内容类型的文件扩展名的函数?
我正在寻找类似的东西:
<?php
function getFileExtension($contentType)
if ($contentType === 'image/png')
return '.png';
elseif ($contentType === 'image/jpg')
return '.jpg';
elseif ($contentType === 'application/zip')
return '.zip';
else
return FALSE;
目标是使用处理所有内容类型的库函数。根据上面的模式,我想我可以用这样的方式自己动手:
<?php
function getFileExtension($contentType)
$pieces = explode('/', $contentType);
return '.' . array_pop($pieces);
...但这似乎很笨拙。有人知道已经编写的 PHP 解决方案吗? LMK。谢谢!
【问题讨论】:
如何知道内容类型?您在提供这些文件吗? 阅读以下内容:php.net/manual/en/book.fileinfo.php @Erik:OP 想要做到这一点的相反。 @MikePurcell 我知道内容类型,因为内容类型与文件内容一起存储在我的数据库中。 @Blender switch 语句(具有默认值)基本上与 if elseif else 语句完全相同。开关不能解决我的问题。 【参考方案1】:鉴于存储在我的应用程序数据库中的文件类型数量有限,以下类/方法是可接受的解决方案。
<?php
class AppUtil
public static function FileExt($contentType)
$map = array(
'application/pdf' => '.pdf',
'application/zip' => '.zip',
'image/gif' => '.gif',
'image/jpeg' => '.jpg',
'image/png' => '.png',
'text/css' => '.css',
'text/html' => '.html',
'text/javascript' => '.js',
'text/plain' => '.txt',
'text/xml' => '.xml',
);
if (isset($map[$contentType]))
return $map[$contentType];
// HACKISH CATCH ALL (WHICH IN MY CASE IS
// PREFERRED OVER THROWING AN EXCEPTION)
$pieces = explode('/', $contentType);
return '.' . array_pop($pieces);
echo AppUtil::FileExt('application/zip'); // prints '.zip'
不过,在确定上述解决方案之前,我在 Google 上进行了一些搜索(如 @whichdan 所建议的那样),并找到了以下“MIME 类型扩展”映射资源。
http://www.webmaster-toolkit.com/mime-types.shtml
在对上述 URL 中提供的数据进行一些分析之后,很明显没有针对此问题的明确解决方案(这可能是 PHP 中没有“文件扩展名的内容类型”功能的原因) .
为了让 Stack Overflow 社区受益,为了说明这一事实,我解析了上述 URL 中提供的数据,并将每种内容类型分为两个类别之一 - “确定”或“模棱两可”。在我的每个“解析后”数组上运行 var_export 会产生以下 PHP 代码。
<?php
// DEFINITIVE "CONTENT TYPE TO FILE EXTENSION" MAPPINGS
$definitive = array (
'application/x-authorware-bin' => '.aab',
'application/x-authorware-map' => '.aam',
'application/x-authorware-seg' => '.aas',
'text/vnd.abc' => '.abc',
'video/animaflex' => '.afl',
'application/x-aim' => '.aim',
'text/x-audiosoft-intra' => '.aip',
'application/x-navi-animation' => '.ani',
'application/x-nokia-9000-communicator-add-on-software' => '.aos',
'application/mime' => '.aps',
'application/arj' => '.arj',
'image/x-jg' => '.art',
'text/asp' => '.asp',
'application/x-mplayer2' => '.asx',
'video/x-ms-asf-plugin' => '.asx',
'audio/x-au' => '.au',
'application/x-troff-msvideo' => '.avi',
'video/avi' => '.avi',
'video/msvideo' => '.avi',
'video/x-msvideo' => '.avi',
'video/avs-video' => '.avs',
'application/x-bcpio' => '.bcpio',
'application/mac-binary' => '.bin',
'application/macbinary' => '.bin',
'application/x-binary' => '.bin',
'application/x-macbinary' => '.bin',
'image/x-windows-bmp' => '.bmp',
'application/x-bzip' => '.bz',
'application/vnd.ms-pki.seccat' => '.cat',
'application/clariscad' => '.ccad',
'application/x-cocoa' => '.cco',
'application/cdf' => '.cdf',
'application/x-cdf' => '.cdf',
'application/java' => '.class',
'application/java-byte-code' => '.class',
'application/x-java-class' => '.class',
'application/x-cpio' => '.cpio',
'application/mac-compactpro' => '.cpt',
'application/x-compactpro' => '.cpt',
'application/x-cpt' => '.cpt',
'application/pkcs-crl' => '.crl',
'application/pkix-crl' => '.crl',
'application/x-x509-user-cert' => '.crt',
'application/x-csh' => '.csh',
'text/x-script.csh' => '.csh',
'application/x-pointplus' => '.css',
'text/css' => '.css',
'application/x-deepv' => '.deepv',
'video/dl' => '.dl',
'video/x-dl' => '.dl',
'application/commonground' => '.dp',
'application/drafting' => '.drw',
'application/x-dvi' => '.dvi',
'drawing/x-dwf (old)' => '.dwf',
'model/vnd.dwf' => '.dwf',
'application/acad' => '.dwg',
'application/dxf' => '.dxf',
'text/x-script.elisp' => '.el',
'application/x-bytecode.elisp (compiled elisp)' => '.elc',
'application/x-elc' => '.elc',
'application/x-esrehber' => '.es',
'text/x-setext' => '.etx',
'application/envoy' => '.evy',
'application/vnd.fdf' => '.fdf',
'application/fractals' => '.fif',
'image/fif' => '.fif',
'video/fli' => '.fli',
'video/x-fli' => '.fli',
'text/vnd.fmi.flexstor' => '.flx',
'video/x-atomic3d-feature' => '.fmf',
'image/vnd.fpx' => '.fpx',
'image/vnd.net-fpx' => '.fpx',
'application/freeloader' => '.frl',
'image/g3fax' => '.g3',
'image/gif' => '.gif',
'video/gl' => '.gl',
'video/x-gl' => '.gl',
'application/x-gsp' => '.gsp',
'application/x-gss' => '.gss',
'application/x-gtar' => '.gtar',
'multipart/x-gzip' => '.gzip',
'application/x-hdf' => '.hdf',
'text/x-script' => '.hlb',
'application/hlp' => '.hlp',
'application/x-winhelp' => '.hlp',
'application/binhex' => '.hqx',
'application/binhex4' => '.hqx',
'application/mac-binhex' => '.hqx',
'application/mac-binhex40' => '.hqx',
'application/x-binhex40' => '.hqx',
'application/x-mac-binhex40' => '.hqx',
'application/hta' => '.hta',
'text/x-component' => '.htc',
'text/webviewhtml' => '.htt',
'x-conference/x-cooltalk' => '.ice ',
'image/x-icon' => '.ico',
'application/x-ima' => '.ima',
'application/x-httpd-imap' => '.imap',
'application/inf' => '.inf ',
'application/x-internett-signup' => '.ins',
'application/x-ip2' => '.ip ',
'video/x-isvideo' => '.isu',
'audio/it' => '.it',
'application/x-inventor' => '.iv',
'i-world/i-vrml' => '.ivr',
'application/x-livescreen' => '.ivy',
'audio/x-jam' => '.jam ',
'application/x-java-commerce' => '.jcm ',
'image/x-jps' => '.jps',
'application/x-javascript' => '.js ',
'image/jutvision' => '.jut',
'music/x-karaoke' => '.kar',
'application/x-ksh' => '.ksh',
'text/x-script.ksh' => '.ksh',
'audio/x-liveaudio' => '.lam',
'application/lha' => '.lha',
'application/x-lha' => '.lha',
'application/x-lisp' => '.lsp ',
'text/x-script.lisp' => '.lsp ',
'text/x-la-asf' => '.lsx',
'application/x-lzh' => '.lzh',
'application/lzx' => '.lzx',
'application/x-lzx' => '.lzx',
'text/x-m' => '.m',
'audio/x-mpequrl' => '.m3u ',
'application/x-troff-man' => '.man',
'application/x-navimap' => '.map',
'application/mbedlet' => '.mbd',
'application/x-magic-cap-package-1.0' => '.mc$',
'application/mcad' => '.mcd',
'application/x-mathcad' => '.mcd',
'image/vasa' => '.mcf',
'text/mcf' => '.mcf',
'application/netmc' => '.mcp',
'application/x-troff-me' => '.me ',
'application/x-frame' => '.mif',
'application/x-mif' => '.mif',
'www/mime' => '.mime ',
'audio/x-vnd.audioexplosion.mjuicemediafile' => '.mjf',
'video/x-motion-jpeg' => '.mjpg ',
'application/x-meme' => '.mm',
'audio/mod' => '.mod',
'audio/x-mod' => '.mod',
'audio/x-mpeg' => '.mp2',
'video/x-mpeq2a' => '.mp2',
'audio/mpeg3' => '.mp3',
'audio/x-mpeg-3' => '.mp3',
'application/vnd.ms-project' => '.mpp',
'application/marc' => '.mrc',
'application/x-troff-ms' => '.ms',
'application/x-vnd.audioexplosion.mzz' => '.mzz',
'application/vnd.nokia.configuration-message' => '.ncm',
'application/x-mix-transfer' => '.nix',
'application/x-conference' => '.nsc',
'application/x-navidoc' => '.nvd',
'application/oda' => '.oda',
'application/x-omc' => '.omc',
'application/x-omcdatamaker' => '.omcd',
'application/x-omcregerator' => '.omcr',
'text/x-pascal' => '.p',
'application/pkcs10' => '.p10',
'application/x-pkcs10' => '.p10',
'application/pkcs-12' => '.p12',
'application/x-pkcs12' => '.p12',
'application/x-pkcs7-signature' => '.p7a',
'application/x-pkcs7-certreqresp' => '.p7r',
'application/pkcs7-signature' => '.p7s',
'text/pascal' => '.pas',
'image/x-portable-bitmap' => '.pbm ',
'application/vnd.hp-pcl' => '.pcl',
'application/x-pcl' => '.pcl',
'image/x-pict' => '.pct',
'image/x-pcx' => '.pcx',
'application/pdf' => '.pdf',
'audio/make.my.funk' => '.pfunk',
'image/x-portable-graymap' => '.pgm',
'image/x-portable-greymap' => '.pgm',
'application/x-newton-compatible-pkg' => '.pkg',
'application/vnd.ms-pki.pko' => '.pko',
'text/x-script.perl' => '.pl',
'application/x-pixclscript' => '.plx',
'text/x-script.perl-module' => '.pm',
'application/x-portable-anymap' => '.pnm',
'image/x-portable-anymap' => '.pnm',
'model/x-pov' => '.pov',
'image/x-portable-pixmap' => '.ppm',
'application/powerpoint' => '.ppt',
'application/x-mspowerpoint' => '.ppt',
'application/x-freelance' => '.pre',
'paleovu/x-pv' => '.pvu',
'text/x-script.phyton' => '.py ',
'applicaiton/x-bytecode.python' => '.pyc ',
'audio/vnd.qcelp' => '.qcp ',
'video/x-qtc' => '.qtc',
'audio/x-realaudio' => '.ra',
'application/x-cmu-raster' => '.ras',
'image/x-cmu-raster' => '.ras',
'text/x-script.rexx' => '.rexx ',
'image/vnd.rn-realflash' => '.rf',
'image/x-rgb' => '.rgb ',
'application/vnd.rn-realmedia' => '.rm',
'audio/mid' => '.rmi',
'application/ringing-tones' => '.rng',
'application/vnd.nokia.ringing-tone' => '.rng',
'application/vnd.rn-realplayer' => '.rnx ',
'image/vnd.rn-realpix' => '.rp ',
'text/vnd.rn-realtext' => '.rt',
'application/x-rtf' => '.rtf',
'video/vnd.rn-realvideo' => '.rv',
'audio/s3m' => '.s3m ',
'application/x-lotusscreencam' => '.scm',
'text/x-script.guile' => '.scm',
'text/x-script.scheme' => '.scm',
'video/x-scm' => '.scm',
'application/sdp' => '.sdp ',
'application/x-sdp' => '.sdp ',
'application/sounder' => '.sdr',
'application/sea' => '.sea',
'application/x-sea' => '.sea',
'application/set' => '.set',
'application/x-sh' => '.sh',
'text/x-script.sh' => '.sh',
'audio/x-psid' => '.sid',
'application/x-sit' => '.sit',
'application/x-stuffit' => '.sit',
'application/x-seelogo' => '.sl ',
'audio/x-adpcm' => '.snd',
'application/solids' => '.sol',
'application/x-pkcs7-certificates' => '.spc ',
'application/futuresplash' => '.spl',
'application/streamingmedia' => '.ssm ',
'application/vnd.ms-pki.certstore' => '.sst',
'application/sla' => '.stl',
'application/vnd.ms-pki.stl' => '.stl',
'application/x-navistyle' => '.stl',
'application/x-sv4cpio' => '.sv4cpio',
'application/x-sv4crc' => '.sv4crc',
'x-world/x-svr' => '.svr',
'application/x-shockwave-flash' => '.swf',
'application/x-tar' => '.tar',
'application/toolbook' => '.tbk',
'application/x-tcl' => '.tcl',
'text/x-script.tcl' => '.tcl',
'text/x-script.tcsh' => '.tcsh',
'application/x-tex' => '.tex',
'application/plain' => '.text',
'application/gnutar' => '.tgz',
'audio/tsp-audio' => '.tsi',
'application/dsptype' => '.tsp',
'audio/tsplayer' => '.tsp',
'text/tab-separated-values' => '.tsv',
'text/x-uil' => '.uil',
'application/i-deas' => '.unv',
'application/x-ustar' => '.ustar',
'multipart/x-ustar' => '.ustar',
'application/x-cdlink' => '.vcd',
'text/x-vcalendar' => '.vcs',
'application/vda' => '.vda',
'video/vdo' => '.vdo',
'application/groupwise' => '.vew ',
'application/vocaltec-media-desc' => '.vmd ',
'application/vocaltec-media-file' => '.vmf',
'audio/voc' => '.voc',
'audio/x-voc' => '.voc',
'video/vosaic' => '.vos',
'audio/voxware' => '.vox',
'audio/x-twinvq' => '.vqf',
'application/x-vrml' => '.vrml',
'x-world/x-vrt' => '.vrt',
'application/wordperfect6.1' => '.w61',
'audio/wav' => '.wav',
'audio/x-wav' => '.wav',
'application/x-qpro' => '.wb1',
'image/vnd.wap.wbmp' => '.wbmp',
'application/vnd.xara' => '.web',
'application/x-123' => '.wk1',
'windows/metafile' => '.wmf',
'text/vnd.wap.wml' => '.wml',
'application/vnd.wap.wmlc' => '.wmlc ',
'text/vnd.wap.wmlscript' => '.wmls',
'application/vnd.wap.wmlscriptc' => '.wmlsc ',
'application/x-wpwin' => '.wpd',
'application/x-lotus' => '.wq1',
'application/mswrite' => '.wri',
'application/x-wri' => '.wri',
'text/scriplet' => '.wsc',
'application/x-wintalk' => '.wtk ',
'image/x-xbitmap' => '.xbm',
'image/x-xbm' => '.xbm',
'image/xbm' => '.xbm',
'video/x-amt-demorun' => '.xdr',
'xgl/drawing' => '.xgz',
'image/vnd.xiff' => '.xif',
'audio/xm' => '.xm',
'application/xml' => '.xml',
'text/xml' => '.xml',
'xgl/movie' => '.xmz',
'application/x-vnd.ls-xpix' => '.xpix',
'image/xpm' => '.xpm',
'video/x-amt-showrun' => '.xsr',
'image/x-xwd' => '.xwd',
'image/x-xwindowdump' => '.xwd',
'application/x-compress' => '.z',
'application/x-zip-compressed' => '.zip',
'application/zip' => '.zip',
'multipart/x-zip' => '.zip',
'text/x-script.zsh' => '.zsh',
);
...和...
<?php
// AMBIGUOUS "CONTENT TYPE TO FILE EXTENSION" MAPPINGS
$ambiguous = array (
'x-world/x-3dmf' =>
array (
0 => '.3dm',
1 => '.3dmf',
2 => '.qd3 ',
3 => '.qd3d ',
),
'application/octet-stream' =>
array (
0 => '.a',
1 => '.arc',
2 => '.arj',
3 => '.bin',
4 => '.com',
5 => '.dump',
6 => '.exe',
7 => '.lha',
8 => '.lhx',
9 => '.lzh',
10 => '.lzx',
11 => '.o',
12 => '.psd',
13 => '.saveme',
14 => '.uu',
15 => '.zoo',
),
'text/html' =>
array (
0 => '.acgi',
1 => '.htm',
2 => '.html',
3 => '.htmls',
4 => '.htx ',
5 => '.shtml ',
),
'application/postscript' =>
array (
0 => '.ai',
1 => '.eps',
2 => '.ps',
),
'audio/aiff' =>
array (
0 => '.aif',
1 => '.aifc',
2 => '.aiff',
),
'audio/x-aiff' =>
array (
0 => '.aif',
1 => '.aifc',
2 => '.aiff',
),
'video/x-ms-asf' =>
array (
0 => '.asf',
1 => '.asx',
),
'text/x-asm' =>
array (
0 => '.asm',
1 => '.s',
),
'audio/basic' =>
array (
0 => '.au',
1 => '.snd',
),
'image/bmp' =>
array (
0 => '.bm',
1 => '.bmp',
),
'application/book' =>
array (
0 => '.boo',
1 => '.book',
),
'application/x-bzip2' =>
array (
0 => '.boz',
1 => '.bz2',
),
'application/x-bsh' =>
array (
0 => '.bsh',
1 => '.sh',
2 => '.shar',
),
'text/plain' =>
array (
0 => '.c',
1 => '.c++',
2 => '.cc',
3 => '.com',
4 => '.conf',
5 => '.cxx',
6 => '.def',
7 => '.f',
8 => '.f90',
9 => '.for',
10 => '.g',
11 => '.h',
12 => '.hh',
13 => '.idc',
14 => '.jav',
15 => '.java',
16 => '.list',
17 => '.log ',
18 => '.lst ',
19 => '.m',
20 => '.mar',
21 => '.pl',
22 => '.sdml',
23 => '.text',
24 => '.txt',
),
'text/x-c' =>
array (
0 => '.c',
1 => '.cc',
2 => '.cpp',
),
'application/x-netcdf' =>
array (
0 => '.cdf',
1 => '.nc',
),
'application/pkix-cert' =>
array (
0 => '.cer',
1 => '.crt',
),
'application/x-x509-ca-cert' =>
array (
0 => '.cer',
1 => '.crt',
2 => '.der',
),
'application/x-chat' =>
array (
0 => '.cha',
1 => '.chat',
),
'application/x-director' =>
array (
0 => '.dcr',
1 => '.dir',
2 => '.dxr',
),
'video/x-dv' =>
array (
0 => '.dif',
1 => '.dv',
),
'application/msword' =>
array (
0 => '.doc',
1 => '.dot',
2 => '.w6w',
3 => '.wiz',
4 => '.word ',
),
'image/vnd.dwg' =>
array (
0 => '.dwg',
1 => '.dxf',
2 => '.svf',
),
'image/x-dwg' =>
array (
0 => '.dwg',
1 => '.dxf',
2 => '.svf',
),
'application/x-envoy' =>
array (
0 => '.env',
1 => '.evy',
),
'text/x-fortran' =>
array (
0 => '.f',
1 => '.f77',
2 => '.f90',
3 => '.for',
),
'image/florian' =>
array (
0 => '.flo',
1 => '.turbot',
),
'audio/make' =>
array (
0 => '.funk',
1 => '.my',
2 => '.pfunk',
),
'audio/x-gsm' =>
array (
0 => '.gsd',
1 => '.gsm',
),
'application/x-compressed' =>
array (
0 => '.gz',
1 => '.tgz',
2 => '.z',
3 => '.zip',
),
'application/x-gzip' =>
array (
0 => '.gz',
1 => '.gzip',
),
'text/x-h' =>
array (
0 => '.h',
1 => '.hh',
),
'application/x-helpfile' =>
array (
0 => '.help',
1 => '.hlp',
),
'application/vnd.hp-hpgl' =>
array (
0 => '.hgl',
1 => '.hpg',
2 => '.hpgl',
),
'image/ief' =>
array (
0 => '.ief',
1 => '.iefs',
),
'application/iges' =>
array (
0 => '.iges',
1 => '.igs',
),
'model/iges' =>
array (
0 => '.iges ',
1 => '.igs',
),
'text/x-java-source' =>
array (
0 => '.jav',
1 => '.java ',
),
'image/jpeg' =>
array (
0 => '.jfif',
1 => '.jfif-tbnl',
2 => '.jpe',
3 => '.jpeg',
4 => '.jpg ',
),
'image/pjpeg' =>
array (
0 => '.jfif',
1 => '.jpe',
2 => '.jpeg',
3 => '.jpg ',
),
'audio/midi' =>
array (
0 => '.kar',
1 => '.mid',
2 => '.midi',
),
'audio/nspaudio' =>
array (
0 => '.la ',
1 => '.lma',
),
'audio/x-nspaudio' =>
array (
0 => '.la ',
1 => '.lma',
),
'application/x-latex' =>
array (
0 => '.latex ',
1 => '.ltx',
),
'video/mpeg' =>
array (
0 => '.m1v',
1 => '.m2v',
2 => '.mp2',
3 => '.mp3',
4 => '.mpa',
5 => '.mpe',
6 => '.mpeg',
7 => '.mpg',
),
'audio/mpeg' =>
array (
0 => '.m2a',
1 => '.mp2',
2 => '.mpa',
3 => '.mpg',
4 => '.mpga',
),
'message/rfc822' =>
array (
0 => '.mht',
1 => '.mhtml',
2 => '.mime ',
),
'application/x-midi' =>
array (
0 => '.mid',
1 => '.midi',
),
'audio/x-mid' =>
array (
0 => '.mid',
1 => '.midi',
),
'audio/x-midi' =>
array (
0 => '.mid',
1 => '.midi',
),
'music/crescendo' =>
array (
0 => '.mid',
1 => '.midi',
),
'x-music/x-midi' =>
array (
0 => '.mid',
1 => '.midi',
),
'application/base64' =>
array (
0 => '.mm',
1 => '.mme',
),
'video/quicktime' =>
array (
0 => '.moov',
1 => '.mov',
2 => '.qt',
),
'video/x-sgi-movie' =>
array (
0 => '.movie',
1 => '.mv',
),
'video/x-mpeg' =>
array (
0 => '.mp2',
1 => '.mp3',
),
'application/x-project' =>
array (
0 => '.mpc',
1 => '.mpt',
2 => '.mpv',
3 => '.mpx',
),
'image/naplps' =>
array (
0 => '.nap',
1 => '.naplps',
),
'image/x-niff' =>
array (
0 => '.nif',
1 => '.niff',
),
'application/pkcs7-mime' =>
array (
0 => '.p7c',
1 => '.p7m',
),
'application/x-pkcs7-mime' =>
array (
0 => '.p7c',
1 => '.p7m',
),
'application/pro_eng' =>
array (
0 => '.part ',
1 => '.prt',
),
'chemical/x-pdb' =>
array (
0 => '.pdb',
1 => '.xyz',
),
'image/pict' =>
array (
0 => '.pic',
1 => '.pict',
),
'image/x-xpixmap' =>
array (
0 => '.pm',
1 => '.xpm',
),
'application/x-pagemaker' =>
array (
0 => '.pm4 ',
1 => '.pm5',
),
'image/png' =>
array (
0 => '.png',
1 => '.x-png',
),
'application/mspowerpoint' =>
array (
0 => '.pot',
1 => '.pps',
2 => '.ppt',
3 => '.ppz',
),
'application/vnd.ms-powerpoint' =>
array (
0 => '.pot',
1 => '.ppa',
2 => '.pps',
3 => '.ppt',
4 => '.pwz ',
),
'image/x-quicktime' =>
array (
0 => '.qif',
1 => '.qti',
2 => '.qtif',
),
'audio/x-pn-realaudio' =>
array (
0 => '.ra',
1 => '.ram',
2 => '.rm',
3 => '.rmm ',
4 => '.rmp',
),
'audio/x-pn-realaudio-plugin' =>
array (
0 => '.ra',
1 => '.rmp',
2 => '.rpm',
),
'image/cmu-raster' =>
array (
0 => '.ras',
1 => '.rast',
),
'application/x-troff' =>
array (
0 => '.roff',
1 => '.t',
2 => '.tr',
),
'text/richtext' =>
array (
0 => '.rt',
1 => '.rtf',
2 => '.rtx',
),
'application/rtf' =>
array (
0 => '.rtf',
1 => '.rtx',
),
'application/x-tbook' =>
array (
0 => '.sbk ',
1 => '.tbk',
),
'text/sgml' =>
array (
0 => '.sgm ',
1 => '.sgml',
),
'text/x-sgml' =>
array (
0 => '.sgm ',
1 => '.sgml',
),
'application/x-shar' =>
array (
0 => '.sh',
1 => '.shar',
),
'text/x-server-parsed-html' =>
array (
0 => '.shtml',
1 => '.ssi',
),
'application/x-koan' =>
array (
0 => '.skd',
1 => '.skm ',
2 => '.skp ',
3 => '.skt ',
),
'application/smil' =>
array (
0 => '.smi ',
1 => '.smil ',
),
'text/x-speech' =>
array (
0 => '.spc ',
1 => '.talk',
),
'application/x-sprite' =>
array (
0 => '.spr',
1 => '.sprite ',
),
'application/x-wais-source' =>
array (
0 => '.src',
1 => '.wsrc',
),
'application/step' =>
array (
0 => '.step',
1 => '.stp',
),
'application/x-world' =>
array (
0 => '.svr',
1 => '.wrl',
),
'application/x-texinfo' =>
array (
0 => '.texi',
1 => '.texinfo',
),
'image/tiff' =>
array (
0 => '.tif',
1 => '.tiff',
),
'image/x-tiff' =>
array (
0 => '.tif',
1 => '.tiff',
),
'text/uri-list' =>
array (
0 => '.uni',
1 => '.unis',
2 => '.uri',
3 => '.uris',
),
'text/x-uuencode' =>
array (
0 => '.uu',
1 => '.uue',
),
'video/vivo' =>
array (
0 => '.viv',
1 => '.vivo',
),
'video/vnd.vivo' =>
array (
0 => '.viv',
1 => '.vivo',
),
'audio/x-twinvq-plugin' =>
array (
0 => '.vqe',
1 => '.vql',
),
'model/vrml' =>
array (
0 => '.vrml',
1 => '.wrl',
2 => '.wrz',
),
'x-world/x-vrml' =>
array (
0 => '.vrml',
1 => '.wrl',
2 => '.wrz',
),
'application/x-visio' =>
array (
0 => '.vsd',
1 => '.vst',
2 => '.vsw ',
),
'application/wordperfect6.0' =>
array (
0 => '.w60',
1 => '.wp5',
),
'application/wordperfect' =>
array (
0 => '.wp',
1 => '.wp5',
2 => '.wp6 ',
3 => '.wpd',
),
'application/excel' =>
array (
0 => '.xl',
1 => '.xla',
2 => '.xlb',
3 => '.xlc',
4 => '.xld ',
5 => '.xlk',
6 => '.xll',
7 => '.xlm',
8 => '.xls',
9 => '.xlt',
10 => '.xlv',
11 => '.xlw',
),
'application/x-excel' =>
array (
0 => '.xla',
1 => '.xlb',
2 => '.xlc',
3 => '.xld ',
4 => '.xlk',
5 => '.xll',
6 => '.xlm',
7 => '.xls',
8 => '.xlt',
9 => '.xlv',
10 => '.xlw',
),
'application/x-msexcel' =>
array (
0 => '.xla',
1 => '.xls',
2 => '.xlw',
),
'application/vnd.ms-excel' =>
array (
0 => '.xlb',
1 => '.xlc',
2 => '.xll',
3 => '.xlm',
4 => '.xls',
5 => '.xlw',
),
);
【讨论】:
您不应该将点放在每个值中,您最终只会在某个地方丢失它,并且会给您的代码增加额外的噪音。在 return 语句中或在您构建文件名的位置将点与扩展名连接起来。【参考方案2】:创建自己的类来处理这个问题非常容易。
Google search for "mimetype file extension list"
创建一个静态类,将mimetype => 扩展列表存储在一个数组中,然后编写一个findByMimeType 和findByExtension 方法。您可以使用array_search 按值搜索。
【讨论】:
以上是关于PHP 是不是具有在给定有效内容类型的情况下返回正确文件扩展名的函数?的主要内容,如果未能解决你的问题,请参考以下文章