views:

183

answers:

2

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 directory.

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.

+1  A: 

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!).

leonbloy
Yeah, that's pretty much what I want to do. How does mime_content_type work? I assume it doesn't look at anything related to apache at all...
Ed Marty
`mime_content_type` is a) deprecated, and b) only uses information from a static `magic.mime` file, which doesn't consider your `.htaccess` specifics. see http://php.net/function.mime-content-type .
ax
+1  A: 

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).

ax