why some mp3s on mime_content_type return application/octet-stream

后端 未结 3 1115
北海茫月
北海茫月 2020-12-17 23:48

Why is it that on some mp3s files, when I call mime_content_type($mp3_file_path) it returns application/octet-stream?

This is my code:

if (!empty($_FIL         


        
相关标签:
3条回答
  • 2020-12-17 23:57

    Fleep is the answer to this question. Allowing application/octet-stream is dangerous since .exe and other dangerous files can display with that mime type.

    See this answer https://stackoverflow.com/a/52570299/14482130

    0 讨论(0)
  • 2020-12-17 23:58

    application/octet-stream is probably mime_content_type s fallback type when it fails to recognize a file.

    The MP3 in that case is either not a real MP3 file, or - more likely - the file is a real MP3 file, but does not contain the "magic bytes" the PHP function uses to recognize the format - maybe because it's a different sub-format or has a variable bitrate or whatever.

    You could try whether getid3 gives you better results. I've never worked with it but it looks like a pretty healthy library to get lots of information out of multimedia files.

    If you have access to PHP's configuration, you may also be able to change the mime.magic file PHP uses, although I have no idea whether a better file exists that is able to detect your MP3s. (The mime.magic file is the file containing all the byte sequences that mime_content_type uses to recognize certain file types.)

    0 讨论(0)
  • 2020-12-18 00:09

    MP3 files are a strange beast when it comes to identifying them. You can have an MP3 stored with a .wav container. There can be an ID3v2 header at the start of the file. You can embed an MP3 essentially within any file.

    The only way to detect them reliably is to parse slowly through the file and try to find something that looks like an MP3 frame. A frame is the smallest unit of valid MP3 data possible, and represents (going off memory) 0.028 seconds of audio. The size of the frame varies based on bitrate and sampling rate, so you can't just grab the bitrate/sample rate of the first frame and assume all the other frames will be the same size - a VBR mp3 must be parsed in its entirety to calculate the total playing time.

    All this boils down to that identifying an MP3 by using PHP's fileinfo and the like isn't reliable, as the actual MP3 data can start ANYWHERE in a file. fileinfo only looks at the first kilobyte or two of data, so if it says it's not an MP3, it might very well be lying because the data started slightly farther in.

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