php mime_content_type() 函数不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14809054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:00:42  来源:igfitidea点击:

mime_content_type() function not working

php

提问by user1898114

i am using mime_content_type() function for file upload, localhost it work correctly but it show some error in online file

我正在使用 mime_content_type() 函数上传文件,本地主机它工作正常,但它在在线文件中显示一些错误

Fatal error: Call to undefined function mime_content_type()

致命错误:调用未定义的函数 mime_content_type()

回答by Rao

Update:

更新

mime_content_type()is no longer deprecated, php7 has support for this function now.

mime_content_type()不再被弃用,php7 现已支持此功能。

Earlier version of my answer:

我的回答的早期版本:

mime_content_type()is deprecated, probably because [fileinfo][1] can give you those information about the file and more.

You can use finfo() like shown below,

function _mime_content_type($filename) {
    $result = new finfo();

    if (is_resource($result) === true) {
        return $result->file($filename, FILEINFO_MIME_TYPE);
    }

    return false;
}

Ref: https://stackoverflow.com/a/1263977/1161412

[1]: http://php.net/manual/en/class.finfo.php

mime_content_type()已弃用,可能是因为 [fileinfo][1] 可以为您提供有关该文件的信息以及更多信息。

您可以使用 finfo() 如下所示,

function _mime_content_type($filename) {
    $result = new finfo();

    if (is_resource($result) === true) {
        return $result->file($filename, FILEINFO_MIME_TYPE);
    }

    return false;
}

参考:https: //stackoverflow.com/a/1263977/1161412

[1]:http: //php.net/manual/en/class.finfo.php

回答by Techie

You must have the mime_magic extension on. Check your php.ini and look in phpinfo(). By the way this function has been deprecated as the PECL extension Fileinfoprovides the same functionality (and more) in a much cleaner way.

您必须打开 mime_magic 扩展。检查您的 php.ini 并查看 phpinfo()。顺便说一下,此功能已被弃用,因为 PECL 扩展Fileinfo以更简洁的方式提供相同的功能(以及更多功能)。

Windows users must include the bundled php_fileinfo.dll DLL file in php.ini to enable this extension.

The libmagic library is bundled with PHP, but includes PHP specific changes. A patch against libmagic named libmagic.patch is maintained and may be found within the PHP fileinfo extensions source.

Windows 用户必须在 php.ini 中包含捆绑的 php_fileinfo.dll DLL 文件才能启用此扩展。

libmagic 库与 PHP 捆绑在一起,但包含特定于 PHP 的更改。一个名为 libmagic.patch 的针对 libmagic 的补丁得到维护,可以在 PHP 文件信息扩展源中找到。

Read more

阅读更多

回答by Nayi

If you are on shared hosting, chances are that the fileinfoPHP extensionis either not enabled or installed.

如果您使用的是共享主机,则可能未启用或安装fileinfoPHP 扩展

In the case where it's not enabled, navigate to the Softwaresection of CPanel(consult documentation of your control panel if you're not using CPanel) and click Select PHP Version(or something related to that) and enable the extension by checking its box and saving your action.

如果未启用,请导航到CPanel的“软件”部分(如果您不使用 CPanel,请参阅控制面板的文档)并单击“选择 PHP 版本”(或与之相关的内容)并通过选中其框启用扩展并保存您的操作。

If it's not installed, the extension won't be part of the PHP extensions at cPanel > Software > Select PHP Version > Extensions, edit your php.inifile and uncomment extension=php_fileinfo.dllif you're on Windows. Consult your hosting provider's docs if any of these don't work.

如果未安装,该扩展将不会成为 PHP 扩展的一部分,如果您使用的是 Windows cPanel > Software > Select PHP Version > Extensions,请编辑您的php.ini文件并取消注释extension=php_fileinfo.dll。如果其中任何一个不起作用,请查阅您的托管服务提供商的文档。

回答by shini

I changed my php version from 7.1 to 5.6 it works for me

我将我的 php 版本从 7.1 更改为 5.6 它对我有用

回答by Tomiwa Adefokun

Likely missing \php\extras\magic.mime file.

可能缺少 \php\extras\magic.mime 文件。