/** * {@inheritdoc} */ public function mimetype($path) { $mimeType = null; if (class_exists('Finfo') && $this->exists($path)) { $finfo = new Finfo(FILEINFO_MIME_TYPE); $mimeType = $finfo->file($path); } if (empty($mimeType) || $mimeType === 'text/plain') { $extension = $this->extension($path); if ($extension) { $mimeType = MimeType::detectByFileExtension($extension) ?: 'text/plain'; } } return $mimeType; }
/** * {@inheritdoc} */ public function mimetype($path) { $mimeType = null; if (class_exists('Finfo') && $this->exists($path)) { $finfo = new Finfo(FILEINFO_MIME_TYPE); try { $mimeType = $finfo->file($path); } catch (\Exception $e) { // Gracefully handle non-standard filetypes } } if (empty($mimeType) || $mimeType === 'text/plain') { $extension = $this->extension($path); if ($extension) { $mimeType = MimeType::detectByFileExtension($extension) ?: 'text/plain'; } } return $mimeType; }
/** * Guess MIME Type based on the path of the file and it's content. * * @param string $path * @param string $content * @return mixed MIME Type or NULL if no extension detected */ public static function guessMimeType($path, $content) { $mimeType = MimeType::detectByContent($content); if (empty($mimeType) || $mimeType === 'text/plain') { $extension = pathinfo($path, PATHINFO_EXTENSION); if ($extension) { $mimeType = MimeType::detectByFileExtension($extension) ?: 'text/plain'; } } return $mimeType; }