Programing

파일의 MIME 유형 (Content-Type)을 어떻게 찾을 수 있습니까?

lottogame 2020. 9. 2. 20:34
반응형

파일의 MIME 유형 (Content-Type)을 어떻게 찾을 수 있습니까?


Linux bash 스크립트에서 파일의 MIME 유형 (또는 "Content-Type"이라고 함)을 찾는 방법이 있습니까?

내가 필요한 이유는 어떤 이유로 .png 파일을 파일로 감지하기 때문에 ImageShack이 파일을 업로드하는 데 필요한 것으로 보이기 때문 application/octet-stream입니다.

파일을 확인했는데 실제로는 PNG 이미지입니다.

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

이것은 나에게 오류를 제공합니다.

$ 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>

작동하지만 MIME-TYPE을 지정해야합니다.

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

사용 file. 예 :

> 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

사용할 수있는 다른 도구 (파일 외에) 중 하나는 xdg-mime

예 : xdg-mime query filetype <file>

얌이 있다면

yum install xdg-utils.noarch

Subrip (자막) 파일에서 xdg-mime과 파일의 비교 예

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

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

위 파일에서는 일반 텍스트로만 표시합니다.


파일 버전 <5 : file -i -b / path / to / file
파일 버전> = 5 : file --mime-type -b / path / to / file


옵션으로 file명령을 시도하십시오 -i.

-ioption file 명령이 기존의 사람이 읽을 수있는 문자열이 아닌 MIME 유형 문자열을 출력하도록합니다. 따라서 text/plain; charset=us-ascii오히려 ASCII text.


--mime 파일은 작동하지만 --mime-type은 작동하지 않습니다. 적어도 내 RHEL 5의 경우.

참고 URL : https://stackoverflow.com/questions/2227182/how-can-i-find-out-a-files-mime-type-content-type

반응형