programing

파일의 MIME 유형(Content-Type)을 확인하려면 어떻게 해야 합니까?

bestprogram 2023. 4. 12. 23:05

파일의 MIME 유형(Content-Type)을 확인하려면 어떻게 해야 합니까?

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

필요한 이유는 ImageShack이 파일을 업로드하기 위해 필요한 것 같기 때문입니다.어떤 이유로 인해 .png 파일은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

사용할 수 있는 기타 툴(파일명)의1개는 다음과 같습니다.xdg-mime

xdg-mime query filetype <file>

맛있는 거 있으면

yum install xdg-utils.noarch

서브립(부제) 파일의 xdg-mime과 파일 비교 예

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

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

위의 파일에서는 플레인 텍스트로만 표시됩니다.

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

다음 명령어를 사용해 보십시오.-i선택.

-i옵션을 선택하면 file 명령어는 기존의 사람이 읽을 수 있는 문자열이 아닌 MIME 유형 문자열을 출력합니다.이렇게 말할지도 모른다text/plain; charset=us-ascii보다는ASCII text.

file --filename은 동작하지만 --filename-type은 동작하지 않습니다.적어도 내 RHEL 5는.

MIME 유형을 검출하려면 적절한 이름의 "mimtype" 명령을 사용합니다.

출력 포맷을 위한 다양한 옵션이 있으며 "파일"에 대한 하위 호환성 옵션도 있습니다.

그러나 무엇보다도 파일뿐만 아니라 stdin/pipe를 통해 입력을 받기 때문에 스트림 처리 시 임시 파일을 피할 수 있습니다.

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