简体   繁体   English

如何确定htaccess在PHP中设置的MIME类型

[英]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 directory. 我设置了一个.htaccess文件,以在目录root / a / b /中定义特定的MIME类型,并且所有文件都在同一目录中。

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. 我有一个php文件,该文件想要在根目录/ c /中提供这些文件,并且需要确定.htaccess文件所定义的内容类型。

Is there any way to do this? 有什么办法吗? PHP version is 5.1.6. PHP版本是5.1.6。 mime_content_type returns text/plain , and I'd rather not try to parse the .htaccess file manually. mime_content_type返回text/plain ,我不想尝试手动解析.htaccess文件。 I can move the file if necessary. 如有必要,我可以移动文件。

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? 您的意思是您想通过php脚本提供一些静态文件,并以相同的内容类型返回它们,就像它们是由apache服务器直接(静态)提供的一样?

I don't think you can do that straightforwadly, without reading the .htaccess file. 我认为如果不阅读.htaccess文件,您将无法做到这一点。 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. 甚至那也不是万无一失的,因为.htaccess文件只会覆盖Apache配置的全局设置,甚至覆盖某些父目录中的其他.htaccess文件的全局设置。

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!). 我能想到的唯一简单的方法是,您自己的php脚本建立一个http连接以(静态)获取文件并获取content-type,但这对于您的情况可能不切实际(并且请注意无限循环!)。

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)? 如果您想使用php提供文件,为什么不同时使用php定义mime类型(例如,作为扩展名为=> mime类型对的关联数组)?

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. 也就是说,在不解析.htaccess文件的情况下实现此目标的一种可能方法是让php脚本对root/a/b的文件进行请求,并检查响应头中的mime类型。 if you serve the files with the HTTP wrapper , eg. 如果您使用HTTP包装器提供文件,例如。 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. fopen('http://example.com/a/b/file') ,您将自动获得此信息:您可以通过$http_response_headerstream_get_meta_data访问响应标头$http_response_header类型位于Content-Type:标头中。 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). 如果您以另一种方式打开文件,则仍然可以先执行HEAD请求并检查响应Content-Type(即使对每个服务的文件执行2个请求似乎都过头了)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM