【问题标题】:mime_content_type returning text/plain for css and js files onlymime_content_type 仅返回 css 和 js 文件的文本/纯文本
【发布时间】:2017-12-24 01:30:27
【问题描述】:

我在 PHP 的 mime_content_type 上遇到了这个奇怪的问题,它工作正常,除了 CSS 和 JavaScript 文件,它返回 text/plain (PHP 的默认 mime 设置为 text/html)。

修改 mime.types 文件似乎没有什么区别(它在 httpd.conf 上的路径是正确的)。

例子

<?php
  echo(mime_content_type('index.html')); // returns text/html
  echo(mime_content_type('default.png')); // returns image/png
  echo(mime_content_type('bootstrap.min.js')); // returns text/plain
  echo(mime_content_type('css/animate.css')); // returns text/plain
  echo(mime_content_type('css/style.css')); // returns text/x-asm
?>

在相同的 Apache 安装上提供的相同文件在其 Content-Type 标头上得到正确标记,这几乎就像 PHP 使用其 mime 检测机制一样。

我正在使用Wamp 3.0.6 64-bitWindows 10 64-bit build 15063.483 mod_mime and mode_mime_magic 上的内置PHP 7.0.10 已启用。

【问题讨论】:

    标签: php apache mime


    【解决方案1】:

    PHP 文档对此非常明确:

    返回由使用确定的文件的 MIME 内容类型 来自 magic.mime 文件的信息。

    PHP 和 Apache 不使用相同的 mime 数据库。 Apache 使用 mime.types,但 PHP 使用 magic.mime(不记得它的位置,我认为在 Unix 系统上是 /etc/

    不知道如何在 Windows 上编辑它,但这里有一些适用于 Linux 的提示:How to create a custom magic file database

    【讨论】:

    • 您,先生,很准确,看来我只能做 Apache 所做的事情了,通过文件扩展名猜出正确的 mime。
    猜你喜欢
    • 2020-06-21
    • 2011-06-05
    • 2019-07-11
    • 2014-08-10
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 2014-01-12
    • 2016-11-25
    相关资源
    最近更新 更多