All pastes #394901 Raw Edit

Stuff

public text v1 · immutable
·published 2007-03-14 17:20 UTC
rendered paste body
/*
	Finds the mime-type of a file or says it is octet-stream (download)
	$ext - the file extension
	$filePath - the whole file path (used for fall-back searching)
*/
function mimeType($ext, $filePath = null) {
	switch ($ext) {
		/* Images */
		case 'jpg': case 'JPG': case 'jpeg': case 'JPEG':
			return 'image/jpeg';
		break;

		case 'png': case 'PNG':
			return 'image/png';
		break;

		case 'gif': case 'GIF':
			return 'image/gif';
		break;

		case 'bmp': case 'BMP':
			return 'images/bmp';
		break;

		/* Audio */
		case 'wav': case 'WAV':
			return 'audio/x-wav';
		break;

		case 'mid': case 'MID':
			return 'audio/x-midi';
		break;

		/* Video */
		case 'mpg': case 'MPG': case 'mpeg': case 'MPEG': case 'mpe': case 'MPE':
			return 'video/mpeg';
		break;

		case 'qt': case 'QT': case 'mov': case 'MOV':
			return 'video/quicktime';
		break;

		case 'avi': case 'AVI':
			return 'video/x-msvideo';
		break;

		case 'movie': case 'MOVIE':
			return 'video/x-sgi-movie';
		break;

		/* Application */
		case 'pdf': case 'PDF':
			return 'application/pdf';
		break;

		/* Text */
		case 'css': case 'CSS':
			return 'text/css';
		break;

		case 'js': case 'JS':
			return 'text/javascript';
		break;

		case 'html': case 'HTML': case 'htm': case 'HTM':
			return 'text/html';
		break;

		case 'rtf': case 'RTF': case 'rtx': case 'RTX':
			return 'text/richtext';
		break;

		case 'txt': case 'TXT':
			return 'text/plain';
		break;

		/* Fallback to other detection or octet-stream */
		default:
			if (!empty($filePath) && function_exists('finfo_open')) {
				$fh = finfo_open(FILEINFO_MIME);
				$info = finfo_file($fh, $filePath);
				finfo_close($fh);
				return $info;
			}
			elseif (!empty($filePath) && function_exists('mime_content_type')) {
				return mime_content_type($filePath);
			}
			else {
				return 'application/octet-stream';
			}
		break;
	}
}