How can I find out a files "mime

Is there a way to find out the MIME-TYPE (or is it called "Content-Type"...) of a file in a linux bash script?

The reason I need it is because imageshack appears to need it to upload a file as for some reason it detects the .png file as an "application/octet-stream"…

I'v checked the file, and it really is a .png

$ cat /1.png 
?PNG
(with a heap load of random characters)

This gives me the error:

$ curl -F "fileupload=@/1.png" http://www.imageshack.us/upload_api.php
<links>
<error id="wrong_file_type">Wrong file type detected for file 1.png:application/octet-stream</error>
</links>

This works, but I need to specify a MIME-TYPE.

$ curl -F "fileupload=@/1.png;type=image/png" http://www.imageshack.us/upload_api.php

Use file . Examples:

> file --mime-type image.png
image.png: image/png

> file -b --mime-type image.png
image/png

> file -i FILE_NAME
image.png: image/png; charset=binary

one of the other tool (besides file) you can use is xdg-mime

eg xdg-mime query filetype query filetype <file>

if you have yum,

yum install xdg-utils.noarch

An example comparison of xdg-mime and file on a Subrip(subtitles) file

$ xdg-mime query filetype subtitles.srt
application/x-subrip

$ file --mime-type subtitles.srt
subtitles.srt: text/plain

in the above file only show it as plain text.


file version < 5 : file -i -b /path/to/file
file version >=5 : file --mime-type -b /path/to/file

链接地址: http://www.djcxy.com/p/8248.html

上一篇: 适用于嵌入字体的SVG图像

下一篇: 我怎样才能找到一个文件“mime