/** * Try well-defined methods for getting the MIME type for a file on disk. * First try PECL's Fileinfo library, then try mime_content_type() builtin. * If neither are available, returns NULL. * * @param string file on disk * @return string mime time for given filename, or NULL */ function getMimeTypeFromFile($sFileName) { if (extension_loaded('fileinfo')) { // NOTE: fileinfo doesn't like all magic files. ensure it is pointing to a compatible one if it does not work. // first check the path in the stack $defaultMagicPath = KT_DIR . '/../php/extras/magic'; $defaultMagicPath = realpath($defaultMagicPath); // if not available, attempt path from config if ($defaultMagicPath === false) { $oKTConfig =& KTConfig::getSingleton(); $defaultMagicPath = $oKTConfig->get('magicDatabase'); if (!file_exists($defaultMagicPath)) { $defaultMagicPath = false; } } // attempt file info if magic file is resolved if ($defaultMagicPath) { $res = @finfo_open(FILEINFO_MIME, $defaultMagicPath); $sType = @finfo_file($res, $sFileName); // saw mention that finfo_file() can return empty string under windows if (empty($sType)) { $sType = false; } } } if (!$sType && OS_UNIX) { if (file_exists('/usr/bin/file')) { $aCmd = array('/usr/bin/file', '-bi', $sFileName); $sCmd = KTUtil::safeShellString($aCmd); $sPossibleType = @exec($sCmd); if (preg_match('#^[^/]+/[^/*]+$#', $sPossibleType)) { $sType = $sPossibleType; } } } if ($sType) { $iSpacePos = strpos($sType, ' '); if ($iSpacePos !== false) { $sType = substr($sType, 0, $iSpacePos); } return preg_replace('/;.*$/', '', $sType); } return null; }
<?php require_once "../../../config/dmsDefaults.php"; require_once KT_LIB_DIR . "/util/ktutil.inc"; $aSource = array(array('unzip', "-q", "-j", "-n", "-d", '/tmp', '5 July 2005 Pricelist - Rectron(cpt).zip'), array('unzip', "-q", "-j", "-n", "-d", '/tmp', "5'th July 2005 Pricelist - Rectron(cpt).zip"), array('echo', ''), array('echo', ' ')); $aExpectedResults = array("'unzip' '-q' '-j' '-n' '-d' '/tmp' '5 July 2005 Pricelist - Rectron(cpt).zip'", "'unzip' '-q' '-j' '-n' '-d' '/tmp' '5'\\''th July 2005 Pricelist - Rectron(cpt).zip'", "'echo' ''", "'echo' ' '"); $aResults = array(); foreach ($aSource as $aArgs) { $aResults[] = KTUtil::safeShellString($aArgs); } if ($aResults === $aExpectedResults) { print "Success!\n"; } else { print "Failure!\n"; print "Received: " . print_r($aResults, true) . "\n"; print "Expected: " . print_r($aExpectedResults, true) . "\n"; }