如何在命令行中获取文件的mimetype


how to get mimetype of file in command line

从文件中获取带finfo的mimetype

$finfo = new finfo(FILEINFO_MIME);
$type = $finfo->file($file);

从字符串中获取带finfo的mimetype

$finfo = new finfo(FILEINFO_MIME);
$type = $finfo->buffer($file);

从文件获取命令行中的mimetype

$type = shell_exec('file -bi '.escapeshellarg(realpath($file)));

如何从字符串中获取命令行中的mimetype?

正如你的问题标签上写着"php,linux",你最好试试这个。

$ mimetype <filename> 

示例:

$ mimetype index.php
index.php : application/x-php