Page 1 of 1

Delivering Content-Type when known, but not by file ext

PostPosted: 08. August 2016 15:38
by shortmort37
I run a phpBB board, where attachments are uploaded stored without extension - however, the mime-type is stored in the database. When delivering the attachment for display, I want to be able to specify Content-Type header based on the type, which cannot be derived from the stored filename. Here is a simple example: http://www.59plymouth.net/content-type/test.html

If you click on the left icon where the extension is specified, the video plays. If you click on the right icon sans extension, but where "type" is specified, it downloads the video because the content-type is not known. How do I tell Apache, to deliver the appropriate content-type of "video/mp4" in this particular example? (I get the same behavior with XAMPP on my desktop, but my hosting site runs Apache under linux.)

Here is the code:

Code: Select all
<html>
<body>
<a href="./media/2.mp4"><img src="./media/2_thumb.jpg"/></a>
<a href="./media/2" type="video/mp4"><img src="./media/2_thumb.jpg"/></a>
</body>
</html>


Thanks
Dan

Re: Delivering Content-Type when known, but not by file ext

PostPosted: 08. August 2016 15:59
by shortmort37
Oh, and just to note - I can have mixed media, so it won't do to fix the Content-Type for all references on that page. I want to be able to click a hyperlink, and trigger the transmission of the appropriate Content-Type for the object referenced in that link.

Dan

Re: Delivering Content-Type when known, but not by file ext

PostPosted: 09. August 2016 14:10
by Nobbie
How do you eveluate the Content-Type from the file, if it cannot be derived from the name? How should Apache "know" the Content-Type (as you see, there is no "type" attribute in Links or URLs)? Assume, i upload a file with the name xyz. What is the Conten-Type of that file?

After you know the Content-Type (but i still have no idea how to eveluate from the filename only), you might use a PHP Script to set the appropriate Content-Type for the download (a small PHP Script that triggers the download for all files). But this is step two - first of all i have to know, how to evaluate the Content-Type?

Re: Delivering Content-Type when known, but not by file ext

PostPosted: 09. August 2016 14:24
by shortmort37
When the user clicks on the link in this anchor, the href gets passed to Apache - doesn't the type as well?

<a href="./media/2" type="video/mp4"><img src="./media/2_thumb.jpg"/></a>

What purpose does the endorsed attribute serve, if not to convey content type?

Dan

Re: Delivering Content-Type when known, but not by file ext

PostPosted: 09. August 2016 15:27
by Nobbie
shortmort37 wrote:What purpose does the endorsed attribute serve, if not to convey content type


From http://www.w3schools.com/tags/att_a_type.asp

Note: This attribute is purely advisory.


And (most important) Apache does not "understand" HTML, this attribute is not passed to Apache in any way. The browser generates a HTTP Request from an URL if someone clicks on it, but this request does not contain any Content-Type specification. The type-Attribute might be interpreted by JavaScript or similar (i.e. by the browser), but Apache does not know about that. Its still unsolved, how do you tell Apache which Content-Type does "xyz" have?

shortmort37 wrote:When the user clicks on the link in this anchor, the href gets passed to Apache - doesn't the type as well?


No and no. Href is NOT(!) passed to Apache, instead the Browser parses the contents of href and sends an HTTP request to the server. This request does not contain any type (HTTP does not know any type at this point of HTTP protocoll). Content-Type is a part of the HTTP Header when Apache responses to the request. But it is not part of the request send by the browser.

Re: Delivering Content-Type when known, but not by file ext

PostPosted: 09. August 2016 15:36
by shortmort37
Nobbie wrote:... you might use a PHP Script to set the appropriate Content-Type for the download (a small PHP Script that triggers the download for all files). But this is step two - first of all i have to know, how to evaluate the Content-Type?


Ah.

Server-side, I do know the Content-Type - it's recorded in the phpBB database, along with the filename.

If I retrieve the Content-Type for the object that is requested, how would I set it in the stream with PHP?

Re: Delivering Content-Type when known, but not by file ext

PostPosted: 09. August 2016 15:44
by Altrea