/** * Guess MIME Type based on the path of the file and it's content. * * @param string $path * @param string $content * * @return string|null 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; }
/** * Guess MIME Type based on the path of the file and it's content. * * @param string $path * @param string|resource $content * * @return string|null MIME Type or NULL if no extension detected */ public static function guessMimeType($path, $content) { $mimeType = MimeType::detectByContent($content); if (!(empty($mimeType) || in_array($mimeType, ['application/x-empty', 'text/plain', 'text/x-asm']))) { return $mimeType; } return MimeType::detectByFilename($path); }
/** * Guess MIME Type based on the path of the file and it's content. * * @param string $path * @param string $content * * @return string|null MIME Type or NULL if no extension detected */ public static function guessMimeType($path, $content) { $mimeType = MimeType::detectByContent($content); if (empty($mimeType) || in_array($mimeType, ['application/x-empty', 'text/plain', 'text/x-asm'])) { $extension = pathinfo($path, PATHINFO_EXTENSION); if ($extension) { $mimeType = MimeType::detectByFileExtension($extension) ?: 'text/plain'; } } return $mimeType; }
/** * @param string $path * * @return null|string */ public final function guessMimeType($path) { //@NOTE: The github API does not return a MIME type, so we have to guess :-( if (strrpos($path, '.') > 1) { $extension = substr($path, strrpos($path, '.') + 1); $mimeType = MimeType::detectByFileExtension($extension) ?: 'text/plain'; } else { $content = $this->getFileContents($path); $mimeType = MimeType::detectByContent($content); } return $mimeType; }
/** * @param string $path * * @return null|string * * @throws \Github\Exception\ErrorException * @throws \Github\Exception\InvalidArgumentException * @throws \Github\Exception\RuntimeException */ public final function guessMimeType($path) { //@NOTE: The github API does not return a MIME type, so we have to guess :-( $meta = $this->getMetaData($path); if ($this->hasKey($meta, self::KEY_TYPE) && $meta[self::KEY_TYPE] === self::KEY_DIRECTORY) { $mimeType = self::MIME_TYPE_DIRECTORY; // or application/x-directory } else { $content = $this->getFileContents($path); $mimeType = MimeType::detectByContent($content); } return $mimeType; }