Usage:
getMimeType($filename, $uploadedName) attempts to return a verified mime type.
filename must be the actual location of the file!
uploadedName should be the filename according to the browser
returns null if verification of mime type fails
Note:
should use mime_content_type() in PHP >= 4.3.0
unfortunetly this returns application/octet-stream for too many images, making it's use limited
so the check currently just returns mimetype based on extension :-(
Example:
MimeTypes::getMimeType( $filename ) or die( "go away evil h@x0r!" );
/** * @covers Xoops\Core\MimeTypes::findType */ public function testFindType() { $this->assertEquals('text/plain', $this->object->findType('txt'), 'get mimetype for .txt extension'); $this->assertEquals('image/jpeg', $this->object->findType('jpg'), 'get mimetype for .jpg extension'); $this->assertEquals('image/jpeg', $this->object->findType('jpeg'), 'get mimetype for .jpeg extension'); $this->assertNull($this->object->findType('failme-no-such-extension'), 'get mimetype for garbage extension'); }
public function uploadOSS($input_file_field = 'pic', $save_dir = 'pic', $ext = 'jpg') { $file_path = $_FILES[$input_file_field]["tmp_name"]; if (!$file_path) { FLogger::write('tmp_name为空', 'update_mumu'); return ''; } $file_md5 = md5_file($file_path); $_file_path_info = pathinfo($_FILES[$input_file_field]['name']); $file_ext = strtolower($_file_path_info['extension']); if (!$file_ext) { $this->get_extension($file_path); } if (!$file_ext) { $file_ext = $ext; } $content_type = MimeTypes::get_mimetype(strtolower($file_ext)); $object = 'oss/' . $save_dir . '/' . date('Ym/d/His') . rand(10000, 99999) . '.' . $file_ext; $flag = $this->oss->upload_by_file($this->bucket, 'uploads/' . $object, $file_path, $content_type); if (!$flag) { $object = ''; } $data = array('object' => $object, 'md5' => $file_md5); return $data; }
public function nextFormat() { if ($this->formats === false) { $this->initFormats(); } while (current($this->formatsCurrent) === false) { $key = key($this->formatsCurrent); $nextTypes = $this->formats->next(); if ($nextTypes === false) { return false; } // Base case, ran out of types in ACCEPT string $this->formatsTried = array_merge($this->formatsTried, $this->formatsCurrent); $nextTypes = MimeTypes::formatsFor($nextTypes); $this->formatsCurrent = array(); foreach ($nextTypes as $type) { if (!in_array($type, $this->formatsTried)) { $this->formatsCurrent[] = $type; } } } $result = each($this->formatsCurrent); $this->format = $result[1]; return $result[1]; // Each returns an array of (key, value) }
/** * Responsible for sending all headers in a Response. Marked final because * all headers should be bundled in Response object. * * @param Response $response * @final */ protected function sendHeadersFor(Response $response) { header('HTTP/1.1 ' . ResponseCodes::getMessageForCode($response->code)); $format = $response->request->accepts->format(); header('Content-Type: ' . MimeTypes::preferredMimeTypeFor($format)); foreach ($response->headers as $header) { header($header); } foreach ($response->getCookies() as $cookie) { if ($cookie->value == '') { setcookie($cookie->name, '', time() - 10000, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httponly); } else { setcookie($cookie->name, $cookie->value, $cookie->expire, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httponly); } } flush(); // TODO: Determine other headers to send here. Content-Type, Caching, Etags, ... }
private function getContents() { if (!$this->contents) { $this->validate(); $this->contents = file_get_contents($this->origin); // Obtains the mime type for the contents $fileInfo = new \finfo(FILEINFO_MIME_TYPE); $mimeType = $fileInfo->buffer($this->contents); // With the mimeType try to get the extension $extensions = MimeTypes::extensionsOfMimeType($mimeType); if (!$extensions) { throw new \Exception("No se pudo encontrar una extension para el archivo de origen: " . $mimeType); } $this->extension = $extensions[0]; if (!$this->overwrite) { while (file_exists($this->realDestination())) { $this->fileName = $this->fileName . '_1'; } } } return $this->contents; }
public function uploadOSS($input_file_field = 'pic', $save_dir = 'pic', $ext = 'jpg') { $file_path = $_FILES[$input_file_field]["tmp_name"]; if (!$file_path) { return ''; } $_file_path_info = pathinfo($_FILES[$input_file_field]['name']); $file_ext = strtolower($_file_path_info['extension']); if (!$file_ext) { $this->get_extension($file_path); } if (!$file_ext) { $file_ext = $ext; } $content_type = MimeTypes::get_mimetype(strtolower($file_ext)); $object = 'oss/' . $save_dir . '/' . date('Ym/d/His') . rand(10000, 99999) . '.' . $file_ext; $flag = $this->oss->upload_by_file($this->bucket, 'uploads/' . $object, $file_path, $content_type); if ($flag) { return $object; } else { return ''; } }
/** * 上传文件,适合比较大的文件 * @param string $bucket (Required) * @param string $object (Required) * @param string $file (Required) * @param array $options (Optional) * @author xiaobing.meng@alibaba-inc.com * @since 2012-02-28 * @return ResponseCore */ public function upload_file_by_file($bucket, $object, $file, $options = NULL) { //options $this->validate_options($options); if (!$options) { $options = array(); } //bucket $this->is_empty($bucket, OSS_BUCKET_IS_NOT_ALLOWED_EMPTY); //object $this->is_empty($object, OSS_OBJECT_IS_NOT_ALLOWED_EMPTY); //file $this->is_empty($file, OSS_FILE_PATH_IS_NOT_ALLOWED_EMPTY); if ($this->chk_chinese($file)) { $file = iconv('utf-8', 'gbk', $file); } $options[self::OSS_FILE_UPLOAD] = $file; if (!file_exists($options[self::OSS_FILE_UPLOAD])) { throw new OSS_Exception($options[self::OSS_FILE_UPLOAD] . OSS_FILE_NOT_EXIST); } $filesize = filesize($options[self::OSS_FILE_UPLOAD]); $partsize = 1024 * 1024; //默认为 1M $extension = explode('.', $object); $ext = array_pop($extension); $content_type = MimeTypes::get_mimetype(strtolower($ext)); $options[self::OSS_METHOD] = self::OSS_HTTP_PUT; $options[self::OSS_BUCKET] = $bucket; $options[self::OSS_OBJECT] = $object; $options[self::OSS_CONTENT_TYPE] = $content_type; $options[self::OSS_CONTENT_LENGTH] = $filesize; $response = $this->auth($options); return $response; }
/** * static class returns the appropriate object for the item * @param rsgGallery of the parent gallery * @param array of the database row * @return the apropriate item object */ function getCorrectItemObject(&$gallery, $row) { // get mime type of file $mimetype = MimeTypes::getMimeType($row['name']); // get only the general content type $type = explode('/', $mimetype); $type = $type[0]; if (file_exists(JPATH_RSGALLERY2_ADMIN . '/includes/items/' . $type . '.php')) { require_once JPATH_RSGALLERY2_ADMIN . '/includes/items/' . $type . '.php'; $itemClass = "rsgItem_{$type}"; return new $itemClass($type, $mimetype, $gallery, $row); } else { $itemClass = "rsgItem"; return new $itemClass($type, $mimetype, $gallery, $row); } }
/** * 获取mimetype类型 * @param string $object * @return string */ private function get_mime_type($object) { $extension = explode('.', $object); $extension = array_pop($extension); $mime_type = MimeTypes::get_mimetype(strtolower($extension)); return $mime_type; }
protected function forceFormatFromResourceString(Request $request) { $lastPartIndex = count($request->resourceParts) - 1; if ($lastPartIndex < 0) { return $request; } $lastPart = $request->resourceParts[$lastPartIndex]; $lastDotPosition = strrpos($lastPart, Library::dotSeparator); if ($lastDotPosition !== false) { $format = substr($lastPart, $lastDotPosition + 1); if ($format !== '') { $mime = MimeTypes::preferredMimeTypeFor($format); if ($mime !== false) { $request->accepts->forceFormat($format); $request->format = $format; $request->setResource(substr($request->resource, 0, strrpos($request->resource, Library::dotSeparator))); } } } return $request; }
/** * If the returned mime type is returned for the passed filename without extension. * * @return void */ public function testGetMimeTypeByFilenameWithoutExtension() { $this->assertEquals(MimeTypes::MIMETYPE_DEFAULT, MimeTypes::getMimeTypeByFilename('filename_without_extension')); }
static function init() { // TODO: Cache the MIME Type Data Structure MimeTypes::registerMany(array(array('html', 'text/html'), array('xhtml', 'application/xhtml+xml'), array('xml', array('application/xml', 'text/xml', 'application/x-xml')), array('json', array('application/json', 'text/x-json', 'application/jsonrequest')), array('js', array('text/javascript', 'application/javascript', 'application/x-javascript')), array('jsonp', array('text/javascript')), array('css', 'text/css'), array('rss', 'application/rss+xml'), array('yaml', array('application/x-yaml', 'text/yaml')), array('atom', 'application/atom+xml'), array('text', 'text/plain'), array('png', 'image/png'), array('jpg', 'image/jpeg', 'image/pjpeg'), array('gif', 'image/gif'), array('form', 'multipart/form-data'), array('url-form', 'application/x-www-form-urlencoded'), array('csv', 'text/csv'), array('zip', 'application/zip'))); }
public function testGetExtensions() { $this->assertSame(['jpe', 'jpeg', 'jpg'], MimeTypes::getExtensions('image/jpeg')); }