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

发布于 2024-11-15 06:55:49 字数 432 浏览 2 评论 0原文

我正在寻找的是能够接受给定的任何文件扩展名并返回该扩展名的描述的东西。

例如:

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

echo $description // Outputs 'Portable Network Graphic'

或者

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

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

我搜索了谷歌,但什么也没有出现。如果有人知道这样的脚本是否存在,这将节省大量时间。

提前致谢。

What I'm looking for is something which will take any file extension given to it and return a description of that extension.

For example :

$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'

I've searched google and nothing came up. It would be a huge time saver if anyone knew if such a script existed.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦冥 2024-11-22 06:55:49

是的,有,一个是 getID3 库,还有 PHP 内置的 fileinfo 库

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

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)
        ;
}

Yes there is, one is the getID3 Library and there is the PHP build in fileinfo library.

As the description for everybody is something else, you can create a simple look-up function that works on array data. You only need to add your file-types into the array and you're done:

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)
        ;
}
浪荡不羁 2024-11-22 06:55:49

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

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 /文件格式列表_(按字母顺序排列

It does'nt exist but you can create one.

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

You can find here a complete list of all existing extensions : http://en.wikipedia.org/wiki/List_of_file_formats_(alphabetical)

凶凌 2024-11-22 06:55:49

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

如果您在 Windows 上运行,您还可以使用 SHGetFileInfo 函数来完成您正在寻找的功能。

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

No, there is no easy way to do this in PHP. The closest alternative would be the MIME type which you can get using finfo or mime_content_type.

If you are running on windows you could also use the SHGetFileInfo function which does exactly what you're looking for.

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

鸵鸟症 2024-11-22 06:55:49

如果其他人需要的话,这就是我目前用于上述问题的方法。

/**
 * 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);
}

This is what I am currently using for the above question if anyone else ever needs it.

/**
 * 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);
}
层林尽染 2024-11-22 06:55:49

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

You could use mime types of the file http://www.feedforall.com/mime-types.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文