Determine file type in Ruby

后端 未结 13 1018
耶瑟儿~
耶瑟儿~ 2020-11-30 22:21

How does one reliably determine a file\'s type? File extension analysis is not acceptable. There must be a rubyesque tool similar to the UNIX file(1) comman

相关标签:
13条回答
  • 2020-11-30 23:01

    This was added as a comment on this answer but should really be its own answer:

    path = # path to your file
    
    IO.popen(
      ["file", "--brief", "--mime-type", path],
      in: :close, err: :close
    ) { |io| io.read.chomp }
    

    I can confirm that it worked for me.

    0 讨论(0)
提交回复
热议问题