This method will first try to determine the MIME type based on
finfo_open. If the fileinfo extension is not installed,
it will fall back to BaseFileHelper::getMimeTypeByExtension when $checkExtension is true.
public static getMimeType ( string $file, string $magicFile = null, boolean $checkExtension = true ) : string | ||
$file | string | the file name. |
$magicFile | string | name of the optional magic database file (or alias), usually something like `/path/to/magic.mime`. This will be passed as the second parameter to [finfo_open()](http://php.net/manual/en/function.finfo-open.php) when the `fileinfo` extension is installed. If the MIME type is being determined based via [[getMimeTypeByExtension()]] and this is null, it will use the file specified by [[mimeMagicFile]]. |
$checkExtension | boolean | whether to use the file extension to determine the MIME type in case `finfo_open()` cannot determine it. |
Résultat | string | the MIME type (e.g. `text/plain`). Null is returned if the MIME type cannot be determined. |
/** * upload new media files and link to a given content category id * @menuLabel __HIDDEN__ * @return string */ public function actionUpload($targetCategoryId, $mediaType = null) { if ($mediaType != null && MediaController::$MEDIA_TYPE_AUDIO != $mediaType && MediaController::$MEDIA_TYPE_IMAGE != $mediaType && MediaController::$MEDIA_TYPE_VIDEO != $mediaType) { throw new InvalidParamException('The media type ' . $mediaType . ' is not known.'); } $model = new MediaBrowserImageUpload(); $targetCategoryId = intval($targetCategoryId); $model->targetCategoryId = $targetCategoryId; $msg = ""; if (Yii::$app->request->isPost) { $model->file = UploadedFile::getInstances($model, 'file'); //check if folder exists $cagtegory = CmsContentCategory::findOne($targetCategoryId); if ($cagtegory == null) { throw new InvalidValueException('the given catgeory id is not valid. Upload failed'); } if ($model->file && $model->validate()) { foreach ($model->file as $file) { /* @VAR $file UploadedFile */ $targetPath = $this->getFullUploadPathForFile($file); if ($file->saveAs($targetPath)) { $pathInfo = pathinfo($targetPath); $content = new CmsContentMedia(); $content->init(); $content->mime_type = BaseFileHelper::getMimeType($targetPath); $content->file_name = $pathInfo['basename']; $content->file_path = $pathInfo['dirname']; $content->filesize_bytes = $file->size; $content->content_category_id = $targetCategoryId; $content->media_type = $this->module->getMediaTypeForMimeType($content->mime_type); if ($content->media_type == MediaController::$MEDIA_TYPE_IMAGE) { $dimensions = $this->getImageDimensions($targetPath); if ($dimensions != null) { $content->dimension_width = $dimensions['width']; $content->dimension_height = $dimensions['height']; } else { $msg .= 'Unable to detect image dimensions for image ' . $content->file_name; } } $content->created_datetime = new Expression('NOW()'); $content->createdby_userid = Yii::$app->user->id; if (!$content->insert(true)) { $this->layout = 'modalLayout'; return $this->render('fileUpload', ['model' => $model, 'errors' => $content->errors, 'mediaType' => $mediaType, 'msg' => $msg]); //throw new Exception('The upload of one or more files failed. Most likely validation of properties failed'); } } else { throw new \Exception('The upload of one or more files failed.'); } } //reload browser for current folder return $this->redirect(Url::toRoute(['media/mediabrowser', 'mediatype' => $mediaType, 'activeCategoryId' => $targetCategoryId])); } } $this->layout = 'modalLayout'; return $this->render('fileUpload', ['model' => $model, 'mediaType' => $mediaType, 'msg' => $msg]); }