Menu

Setting the right MIME type when proxying MP4 and M4A through PHP
📰

Setting the right MIME type when proxying MP4 and M4A through PHP

DEV Community: php·Mobeen Ahmed·5 days ago
#eWzbVIOP
#devto#dev#type#header#file
Reading 0:00
15s threshold

Setting the right MIME type when proxying MP4 and M4A through PHP

When I first built the download proxy for ramrom.io, the files arrived fine but the browser did not know what to do with them. A video would open in a blank tab instead of downloading. An audio file would play in the page, or worse, show a page of gibberish characters. The bytes were correct. The header was wrong.

The problem is simple once you see it. PHP sends Content-Type: text/html by default. When my proxy fetched a video from a provider and streamed it straight to the visitor, that default header went along for the ride. The browser looked at the header, decided it was looking at a web page, and acted accordingly. The file extension in the URL did not save me, because the browser trusts the header more than the name.

So the proxy now sets the MIME type itself, based on the file it is serving, before a single byte of the file goes out. MP4 video gets video/mp4 . That one is well known. The one that caught me out is M4A audio.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.