简体   繁体   English

mime_content_type() 函数未定义

[英]mime_content_type() function not defined

I am using the mime_content_type() function for file upload, it works fine on localhost however I'm encountering the following error on my live server:我正在使用mime_content_type()函数上传文件,它在本地主机上运行良好,但是我在我的实时服务器上遇到以下错误:

Fatal error: Call to undefined function mime_content_type()致命错误:调用未定义的函数 mime_content_type()

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. mime_content_type()已弃用,可能是因为 [fileinfo][1] 可以为您提供有关该文件的信息以及更多信息。

You can use finfo() like shown below,您可以使用 finfo() 如下所示,

 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参考: https : //stackoverflow.com/a/1263977/1161412

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

You must have the mime_magic extension on.您必须打开mime_magic扩展。 Check your php.ini and look in phpinfo().检查您的 php.ini 并查看 phpinfo()。 By the way this function has been deprecated as the PECL extension Fileinfo provides the same functionality (and more) in a much cleaner way.顺便说一下,此功能已被弃用,因为 PECL 扩展Fileinfo以更简洁的方式提供相同的功能(以及更多功能)。

Windows users must include the bundled php_fileinfo.dll DLL file in php.ini to enable this extension. Windows 用户必须在 php.ini 中包含捆绑的 php_fileinfo.dll DLL 文件才能启用此扩展。

The libmagic library is bundled with PHP, but includes PHP specific changes. libmagic 库与 PHP 捆绑在一起,但包含特定于 PHP 的更改。 A patch against libmagic named libmagic.patch is maintained and may be found within the PHP fileinfo extensions source.一个名为 libmagic.patch 的针对 libmagic 的补丁得到维护,可以在 PHP 文件信息扩展源中找到。

Read more阅读更多

If you are on shared hosting, chances are that the fileinfo PHP extension is either not enabled or installed.如果您使用的是共享主机,则可能未启用或安装fileinfo PHP 扩展

In the case where it's not enabled, navigate to the Software section 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.ini file and uncomment extension=php_fileinfo.dll if you're on Windows.如果未安装,该扩展将不会成为cPanel > Software > Select PHP Version > Extensions PHP 扩展的一部分,如果您使用的是 Windows,请编辑您的php.ini文件并取消注释extension=php_fileinfo.dll Consult your hosting provider's docs if any of these don't work.如果其中任何一个不起作用,请查阅您的托管服务提供商的文档。

PHP 8 PHP 8

  1. Edit your php.ini file and uncomment extension=fileinfo .编辑您的php.ini文件并取消注释extension=fileinfo

  2. Restart your HTTP server (eg Apache).重新启动您的 HTTP 服务器(例如 Apache)。

  3. echo mime_content_type($path_absolute.$file);//Outputs: application/pdf

PHP 7 PHP 7

  1. Edit your php.ini file and uncomment extension=php_fileinfo.dll .编辑您的php.ini文件并取消注释extension=php_fileinfo.dll

  2. Restart your HTTP server (eg Apache).重新启动您的 HTTP 服务器(例如 Apache)。

  3. echo mime_content_type($path_absolute.$file);//Outputs: application/pdf

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

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

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

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