Determine file type in Ruby

后端 未结 13 1115
耶瑟儿~
耶瑟儿~ 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 22:59

    If you're on a Unix machine try this:

    mimetype = `file -Ib #{path}`.gsub(/\n/,"")
    

    I'm not aware of any pure Ruby solutions that work as reliably as 'file'.

    Edited to add: depending what OS you are running you may need to use 'i' instead of 'I' to get file to return a mime-type.

提交回复
热议问题