note 82855 deleted from function.mime-content-type by cmb

From: Date: Mon, 13 Jun 2016 18:48:40 +0000
Subject: note 82855 deleted from function.mime-content-type by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-205708@lists.php.net to get a copy of this message
Note Submitter: serkanyersen ---- I've written an alternative for this. Not necessarily tested but it works OK on my server. I hope this helps <?php /** * 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]); } } ?>

« previous php.notes (#205708) next »