Is there a file extension to file description script available for PHP?

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.

Answers


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

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)


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


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

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


Need Your Help

How to print a parametrized SQL query?

c# sql .net oledb

How can I get a String with a parametrized SQL query, once parameters inserted ?

IPhone, MPMoviePlayerController how to disable zooming when double tap on the screen?

iphone video mpmovieplayercontroller

How can I dissable the strange double tap behaviour when playing movie using MPMoviePlayerController.