public static function guess(string $path, string $cmd = null): ?string
    {
        if (! \is_file($path)) {
            throw new FileNotFoundException($path);
        }

        if (! \is_readable($path)) {
            throw new AccessDeniedException($path);
        }

        if ($cmd === null) {
            $cmd = 'file -b --mime %s';

            if (\stripos(\PHP_OS, 'win') !== 0) {
                $cmd .= ' 2>/dev/null';
            }
        }

        \ob_start();

        // need to use --mime instead of -i.
        \passthru(\sprintf($cmd, \escapeshellarg($path)), $return);

        if ($return > 0) {
            \ob_end_clean();

            return null;
        }

        $type = \trim((string) \ob_get_clean());

        if (\preg_match('#^([a-z0-9\-]+/[a-z0-9\-\.]+)#i', $type, $match) === false) {
            // it's not a type, but an error message
            return null;
        }

        return $match[1];
    }
public static function guess(string $path, string $cmd = null): ?string
    {
        if (! \is_file($path)) {
            throw new FileNotFoundException($path);
        }

        if (! \is_readable($path)) {
            throw new AccessDeniedException($path);
        }

        if ($cmd === null) {
            $cmd = 'file -b --mime %s';

            if (\stripos(\PHP_OS, 'win') !== 0) {
                $cmd .= ' 2>/dev/null';
            }
        }

        \ob_start();

        // need to use --mime instead of -i.
        \passthru(\sprintf($cmd, \escapeshellarg($path)), $return);

        if ($return > 0) {
            \ob_end_clean();

            return null;
        }

        $type = \trim((string) \ob_get_clean());

        if (\preg_match('#^([a-z0-9\-]+/[a-z0-9\-\.]+)#i', $type, $match) === false) {
            // it's not a type, but an error message
            return null;
        }

        return $match[1];
    }
public static function guess ( string $ path , string $ cmd = null ) : ? string { if ( ! \ is_file ( $ path ) ) { throw new FileNotFoundException ( $ path ) ; } if ( ! \ is_readable ( $ path ) ) { throw new AccessDeniedException ( $ path ) ; } if ( $ cmd === null ) { $ cmd = 'file -b --mime %s' ; if ( \ stripos ( \ PHP_OS , 'win' ) !== 0 ) { $ cmd .= ' 2>/dev/null' ; } } \ ob_start ( ) ; // need to use --mime instead of -i. \ passthru ( \ sprintf ( $ cmd , \ escapeshellarg ( $ path ) ) , $ return ) ; if ( $ return > 0 ) { \ ob_end_clean ( ) ; return null ; } $ type = \ trim ( ( string ) \ ob_get_clean ( ) ) ; if ( \ preg_match ( '#^([a-z0-9\-]+/[a-z0-9\-\.]+)#i' , $ type , $ match ) === false ) { // it's not a type, but an error message return null ; } return $ match [ 1 ] ; }
Guesses the mime type with the binary "file".

@param string $path
@param string $cmd  the command to run to get the mime type of a file.
The $cmd pattern must contain a "%s" string that will be replaced
with the file name to guess.
The command output must start with the mime type of the file.
Like: text/plain; charset=us-ascii

@throws \Narrowspark\MimeType\Exception\AccessDeniedException If the file could not be read

@return null|string
Guesses the mime type with the binary file .