是否有可用于PHP的文件描述脚本的文件扩展名?

时间:2011-06-14 15:11:37

标签: php file-extension

我正在寻找的东西将采用任何文件扩展名并返回该扩展名的描述。

例如:

$extension = 'PNG';
$description = ext_description($extension);

echo $description // Outputs 'Portable Network Graphic'

OR

 $extension = 'DOC';
 $description = ext_description($extension);

 echo $description // Outputs 'Microsoft Office Word Document'

我搜索了谷歌并没有出现任何问题。如果有人知道这样的剧本是否存在,那将节省大量时间。

提前致谢。

5 个答案:

答案 0 :(得分:2)

是的,有一个是getID3 Library and there is the PHP build in fileinfo library

由于每个人的描述都是别的,您可以创建一个适用于数组数据的简单查找功能。您只需要将文件类型添加到数组中即可完成:

function ext_description($extension) {
    static extensions = array(
        'png' => 'Portable Network Graphic',
        'doc' => 'Microsoft Office Word Document'
    );
    $extension = strtolower($extension);
    return isset($extensions[$extension])
        ? $extensions[$extension]
        : sprintf('Unknown File (%s)', $extension)
        ;
}

答案 1 :(得分:2)

它不存在,但您可以创建一个。

function ext_description($extension) {
    switch ($extension) {
    case "png":
        return "Portable Network Graphic";
    case "doc":
        return "Microsoft Office Word Document";
    default:
        return "Unknow extention";
}

您可以在此处找到所有现有扩展程序的完整列表:http://en.wikipedia.org/wiki/List_of_file_formats_(alphabetical

答案 2 :(得分:1)

不,在PHP中没有简单的方法可以做到这一点。最接近的替代方法是使用finfomime_content_type可以获得的MIME类型。

如果您在Windows上运行,您还可以使用SHGetFileInfo功能,它正是您正在寻找的功能。

  

http://msdn.microsoft.com/en-us/library/bb762179(v=vs.85).aspx

答案 3 :(得分:0)

如果其他人需要,我正在使用上述问题。

/**
 * Takes a file extension and returns a readable file type.
 * 
 * @param string $ext
 * @return string
 * @example .doc returns Microsoft Word Document
 */
function file_extension_2_type($ext) {

    $files = json_decode('{".doc":"Microsoft Word Document",".docx":"Microsoft Word Open XML Document",".log":"Log File",".msg":"Outlook Mail Message",".pages":"Pages Document",".rtf":"Rich Text Format File",".txt":"Plain Text File",".wpd":"WordPerfect Document",".wps":"Microsoft Works Word Processor Document",".csv":"Comma Separated Values File",".dat":"Data File",".efx":"eFax Document",".gbr":"Gerber File",".key":"Keynote Presentation",".pps":"PowerPoint Slide Show",".ppt":"PowerPoint Presentation",".pptx":"PowerPoint Open XML Presentation",".sdf":"Standard Data File",".vcf":"vCard File",".xml":"XML File",".aif":"Audio Interchange File Format",".iff":"Interchange File Format",".m3u":"Media Playlist File",".m4a":"MPEG-4 Audio File",".mid":"MIDI File",".mp3":"MP3 Audio File",".mpa":"MPEG-2 Audio File",".wav":"WAVE Audio File",".wma":"Windows Media Audio File",".3g2":"3GPP2 Multimedia File",".3gp":"3GPP Multimedia File",".asf":"Advanced Systems Format File",".asx":"Microsoft ASF Redirector File",".avi":"Audio Video Interleave File",".flv":"Flash Video File",".mov":"Apple QuickTime Movie",".mp4":"MPEG-4 Video File",".mpg":"MPEG Video File",".swf":"Shockwave Flash Movie",".vob":"DVD Video Object File",".wmv":"Windows Media Video File",".3dm":"Rhino 3D Model",".max":"3ds Max Scene File",".bmp":"Bitmap Image File",".gif":"Graphical Interchange Format File",".jpg":"JPEG Image File",".png":"Portable Network Graphic",".psd":"Adobe Photoshop Document",".pspimage":"PaintShop Pro Image",".thm":"Thumbnail Image File",".tif":"Tagged Image File",".yuv":"YUV Encoded Image File",".ai":"Adobe Illustrator File",".drw":"Drawing File",".eps":"Encapsulated PostScript File",".ps":"PostScript File",".svg":"Scalable Vector Graphics File",".indd":"Adobe InDesign Document",".pct":"Picture File",".pdf":"Portable Document Format File",".qxd":"QuarkXPress Document",".qxp":"QuarkXPress Project File",".rels":"Open Office XML Relationships File",".xlr":"Works Spreadsheet",".xls":"Excel Spreadsheet",".xlsx":"Microsoft Excel Open XML Spreadsheet",".accdb":"Access 2007 Database File",".db":"Database File",".dbf":"Database File",".mdb":"Microsoft Access Database",".pdb":"Program Database",".sql":"Structured Query Language Data",".app":"Mac OS X Application",".bat":"DOS Batch File",".cgi":"Common Gateway Interface Script",".com":"DOS Command File",".exe":"Windows Executable File",".gadget":"Windows Gadget",".jar":"Java Archive File",".pif":"Program Information File",".vb":"VBScript File",".wsf":"Windows Script File",".gam":"Saved Game File",".nes":"Nintendo (NES) ROM File",".rom":"N64 Game ROM File",".sav":"Saved Game",".dwg":"AutoCAD Drawing Database File",".dxf":"Drawing Exchange Format File",".gpx":"GPS Exchange File",".kml":"Keyhole Markup Language File",".asp":"Active Server Page",".cer":"Internet Security Certificate",".csr":"Certificate Signing Request File",".css":"Cascading Style Sheet",".htm":"Hypertext Markup Language File",".html":"Hypertext Markup Language File",".js":"JavaScript File",".jsp":"Java Server Page",".php":"Hypertext Preprocessor File",".rss":"Rich Site Summary",".xhtml":"Extensible Hypertext Markup Language File",".8bi":"Photoshop Plug-in",".plugin":"Mac OS X Plug-in",".xll":"Excel Add-In File",".fnt":"Windows Font File",".fon":"Generic Font File",".otf":"OpenType Font",".ttf":"TrueType Font",".cab":"Windows Cabinet File",".cpl":"Windows Control Panel Item",".cur":"Windows Cursor",".dll":"Dynamic Link Library",".dmp":"Windows Memory Dump",".drv":"Device Driver",".lnk":"File Shortcut",".sys":"Windows System File",".cfg":"Configuration File",".ini":"Windows Initialization File",".keychain":"Mac OS X Keychain File",".prf":"Outlook Profile File",".bin":"Macbinary Encoded File",".hqx":"BinHex 4.0 Encoded File",".mim":"Multi-Purpose Internet Mail Message File",".uue":"Uuencoded File",".7z":"7-Zip Compressed File",".deb":"Debian Software Package",".gz":"Gnu Zipped Archive",".pkg":"Mac OS X Installer Package",".rar":"WinRAR Compressed Archive",".rpm":"Red Hat Package Manager File",".sit":"Stuffit Archive",".sitx":"Stuffit X Archive",".tar.gz":"Tarball File",".zip":"Zipped File",".zipx":"Extended Zip File",".dmg":"Mac OS X Disk Image",".iso":"Disc Image File",".toast":"Toast Disc Image",".vcd":"Virtual CD",".c":"C\/C++ Source Code File",".class":"Java Class File",".cpp":"C++ Source Code File",".cs":"Visual C# Source Code File",".dtd":"Document Type Definition File",".fla":"Adobe Flash Animation",".java":"Java Source Code File",".m":"Objective-C Implementation File",".pl":"Perl Script",".py":"Python Script",".bak":"Backup File",".gho":"Norton Ghost Backup File",".ori":"Original File",".tmp":"Temporary File",".dbx":"Outlook Express E-mail Folder",".msi":"Windows Installer Package",".part":"Partially Downloaded File",".torrent":"BitTorrent File"}', true);


    if (isset($files['.' . strtolower($ext)]))
        return $files['.' . strtolower($ext)] . ' (' . strtoupper($ext) . ')';


    return strtoupper($ext);
}

答案 4 :(得分:0)

您可以使用文件http://www.feedforall.com/mime-types.htm

的mime类型