note 82855 added to function.mime-content-type

From: Date: Tue, 29 Apr 2008 15:22:04 +0000
Subject: note 82855 added to function.mime-content-type
Groups: php.notes 
Request: Send a blank email to php-notes+get-138900@lists.php.net to get a copy of this message
I've written an alternative for this. Not necessarily tested but it works OK on my server. I hope this helps <? /** * Tries to get mime data of the file. * @return {String} mime-type of the given file * @param $filename String */ function get_mime($filename){ preg_match("/\.(.*?)$/", $filename, $m); # Get File extension for a better match switch(strtolower($m[1])){ case "js": return "application/javascript"; case "json": return "application/json"; case "jpg": case "jpeg": case "jpe": return "image/jpg"; case "png": case "gif": case "bmp": return "image/".strtolower($m[1]); case "css": return "text/css"; case "xml": return "application/xml"; case "html": case "htm": case "php": return "text/html"; default: if(function_exists("mime_content_type")){ # if mime_content_type exists use it. $m = mime_content_type($filename); }else if(function_exists("")){ # if Pecl installed use it $finfo = finfo_open(FILEINFO_MIME); $m = finfo_file($finfo, $filename); finfo_close($finfo); }else{ # if nothing left try shell if(strstr($_SERVER[HTTP_USER_AGENT], "Windows")){ # Nothing to do on windows return ""; # Blank mime display most files correctly especially images. } if(strstr($_SERVER[HTTP_USER_AGENT], "Macintosh")){ # Correct output on macs $m = trim(exec('file -b --mime '.escapeshellarg($filename))); }else{ # Regular unix systems $m = trim(exec('file -bi '.escapeshellarg($filename))); } } $m = split(";", $m); return trim($m[0]); } } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 88.248.8.227 ---- Manual Page -- http://www.php.net/manual/en/function.mime-content-type.php Edit -- https://master.php.net/note/edit/82855 Del: integrated -- https://master.php.net/note/delete/82855/integrated Del: useless -- https://master.php.net/note/delete/82855/useless Del: bad code -- https://master.php.net/note/delete/82855/bad+code Del: spam -- https://master.php.net/note/delete/82855/spam Del: non-english -- https://master.php.net/note/delete/82855/non-english Del: in docs -- https://master.php.net/note/delete/82855/in+docs Del: other reasons-- https://master.php.net/note/delete/82855 Reject -- https://master.php.net/note/reject/82855 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#138900) next »