note 103646 added to function.mime-content-type
| From: | te at xeony dot de | Date: | Tue, 26 Apr 2011 05:36:15 +0000 |
| Subject: | note 103646 added to function.mime-content-type | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-179133@lists.php.net to get a copy of this message | ||
I've searched a while for some ways to get the MIME Type of a file and nothing makes me happy.
So i wrote a all-in-one variante to get the MIME Type:
<?php
/*
* Usage:
*
* $sMimeType = $oMime::getMimeType('<filepath>');
*
*/
class Mime
{
public static function getMimeType($sFile)
{
if (function_exists('mime_content_type')){
return mime_content_type($sFile);
}
if (function_exists('finfo_file')){
$hFInfo = finfo_open(FILEINFO_MIME);
$mType = finfo_file($hFInfo, $sFile);
finfo_close($hFInfo);
return $mType;
}
if (function_exists('get_headers')) {
$aInfo = get_headers(self::getDomain() . $sFile, 1);
return $aInfo['Content-Type'];
}
if (function_exists('curl_init')) {
$hCURL = curl_init(self::getDomain() . $sFile);
curl_setopt($hCURL, CURLOPT_HEADER, 1);
curl_setopt($hCURL, CURLOPT_NOBODY, 1);
curl_setopt($hCURL, CURLOPT_RETURNTRANSFER, 1);
if (!curl_exec($hCURL)) return false;
$aInfo = curl_getinfo($hCURL);
return $aInfo['content_type'];
}
// Nothing works -.- try to catch it via fsockopen! -.-
$hFP = fsockopen(self::getDomain(false), 80, &$iErrno, &$sErrstr, 30);
if ($hFP) {
$sRequest = "GET " . self::getDomain(true) . $sFile . " HTTP/1.0\r\n";
$sRequest .= "Accept: */*\r\n";
$sRequest .= "Cache-Control: no-cache\r\n";
$sRequest .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.2)
Gecko/2008090514 Firefox/3.0.2\r\n";
$sRequest .= "Host: " . self::getDomain(false) . "\r\n";
$sRequest .= "Connection: Close\r\n";
$sRequest .= "\r\n";
fputs($hFP, $sRequest);
$sLine = '';
$sHeader = '';
while(!($sLine == "\r\n") && !feof($hFP)) {
$sLine = fgets($hFP, 4096);
$sHeader .= $sLine;
}
fclose($hFP);
if (preg_match('/Content-Type: (.*)\n\r/i', $sHeader, $aMatch))
return $aMatch[1];
}
}
public static function getDomain($bProtocol = true, $secure = false) {
$protocol = ((isset($_SERVER['HTTPS']) &&
!empty($_SERVER['HTTPS'])) || $secure === true) ? 'https://' : 'http://';
$domain = $_SERVER['SERVER_NAME'];
return ($bProtocol ? $protocol : '') . $domain;
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 77.8.104.47
----
Manual Page -- http://www.php.net/manual/en/function.mime-content-type.php
Edit -- https://master.php.net/note/edit/103646
Del: integrated -- https://master.php.net/note/delete/103646/integrated
Del: useless -- https://master.php.net/note/delete/103646/useless
Del: bad code -- https://master.php.net/note/delete/103646/bad+code
Del: spam -- https://master.php.net/note/delete/103646/spam
Del: non-english -- https://master.php.net/note/delete/103646/non-english
Del: in docs -- https://master.php.net/note/delete/103646/in+docs
Del: other reasons-- https://master.php.net/note/delete/103646
Reject -- https://master.php.net/note/reject/103646
Search -- https://master.php.net/manage/user-notes.php