开发者

How to determine MIME Type set by htaccess in PHP

I have a .htaccess file set up to define specific MIME types in directory root/a/b/, and all of the files are in the same dir开发者_如何学Goectory.

I have a php file that wants to serve those files, in directory root/c/, and needs to determine the content-type as defined by the .htaccess file.

Is there any way to do this? PHP version is 5.1.6. mime_content_type returns text/plain, and I'd rather not try to parse the .htaccess file manually. I can move the file if necessary.


if you want to serve the files with php, why don't you also define the mime types with php (eg. as an associative array with extension => mime type pairs)?

that being said, a possible way to achieve this without parsing the .htaccess file would be to let the php script do a request for the file in root/a/b and check the mime type in the response headers. if you serve the files with the HTTP wrapper, eg. fopen('http://example.com/a/b/file'), you get this automatically: you can access the response headers via $http_response_header or stream_get_meta_data - the mime type is in the Content-Type: header. if you open the file another way, you can still do a HEAD request first and check the response Content-Type (even though doing 2 requests for every file served seems overkill).


You mean you want to serve some static files through a php script and return them with the same content-type as if they were served directly (statically) by the apache server?

I don't think you can do that straightforwadly, without reading the .htaccess file. And even that might not be foolproof, because the .htaccess file just overrides the global settings of the Apache config, or even of some other .htaccess files in some parent directory.

The only foolproof way I can imagine, is your own php script making a http connection for getting (statically) the file and obtaining the content-type- but this might not be practical for your scenario (and be careful with infinite loops!).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜