0

I want to check if a certain file is playable by the browser.

Right now, I'm doing this:

$playable = ['image/png', 'image/jpeg' ... 'video/mp4' ...];
if (isset($playable[mime_content_type($file)])) {
  // file is playable
}

Is there a better way to do this? Maybe a list of MIME-types which are playable by modern browsers?

aequabit
  • 50
  • 1
  • 7
  • 2
    At the very least, you could switch the conditional to use `in_array` rather than `isset`. As for your primary question, have a look at this: http://stackoverflow.com/questions/18678400/knowing-with-php-if-the-browser-can-play-audio-file-with-html5-audio-tag – Stuart Wagner Feb 05 '17 at 07:48

1 Answers1

0

You could just serve up the content in the appropriate element based on the first half of the mime type. You can then use the onerror event handler to detect errors.

See How does one use the onerror attribute of an img element

Community
  • 1
  • 1
Charlie
  • 8,530
  • 2
  • 55
  • 53