如何在PHP中启用php_fileinfo扩展?

时间:2021-11-16 11:03:46

My Laravel app can't validate uploaded images. It returns this error message:

我的Laravel应用无法验证上传的图片。它返回此错误消息:

Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?)

无法猜测mime类型,因为没有可用的猜测器(你启用了php_fileinfo扩展吗?)

如何在PHP中启用php_fileinfo扩展?

I know enabling php_fileinfo.dll/php_fileinfo.so at php.ini fixes it. But I don't have access to my remote server's php.ini file.

我知道在php.ini上启用php_fileinfo.dll / php_fileinfo.so修复它。但我无法访问我的远程服务器的php.ini文件。

So I thought is there any other way to enable it? And how? Maybe I can use PHP's ini_set() methods? I tried using it like:

所以我认为有没有其他方法来启用它?如何?也许我可以使用PHP的ini_set()方法?我尝试使用它像:

ini_set('extension', 'php_fileinfo.so');

But it doesn't work.

但它不起作用。

I also read you can override php.ini settings using .htaccess files. But how?

我还读到你可以使用.htaccess文件覆盖php.ini设置。但是怎么样?

As Laravel's default, my .htaccess file is:

作为Laravel的默认值,我的.htaccess文件是:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

How should you put such additionals in there?

你应该怎么把这些额外的东西放在那里?

3 个解决方案

#1


1  

Create a .htaccess file where your root is and then do this

创建一个.htaccess文件,其中包含root用户,然后执行此操作

php_value extension=php_fileinfo.dll

or

php_admin_flag extension=php_fileinfo.dll

#2


0  

If you only want to upload images then it might help you.

如果您只想上传图片,那么它可能对您有所帮助。

$image_size_info = @getimagesize($filename); //surpress errors(@) if not image
if( empty($image_size_info) ) $mime_type = "";
else $mime_type = @image_type_to_mime_type( $image_size_info[2] );

//safety for all cases:
if( empty($mime_type) ) $mime_type = "";

if(  strpos($mime_type, "image/") === false  )
{
//not an Image !
} 
else 
{
  //proceed file upload
}

If above code doen't work then it might help you -->> https://github.com/thephpleague/flysystem/commit/0ec96b1104e57bfcf0e40d6169c8e203b7942b34

如果上面的代码不起作用,那么它可能对你有帮助 - >> https://github.com/thephpleague/flysystem/commit/0ec96b1104e57bfcf0e40d6169c8e203b7942b34

#3


0  

If you have cpanel just go to

如果你有cpanel就去

Select PHP Version

选择PHP版本

and search for the extension

并搜索扩展名

fileinfo

check it and you're good to go.

检查它,你很高兴。

#1


1  

Create a .htaccess file where your root is and then do this

创建一个.htaccess文件,其中包含root用户,然后执行此操作

php_value extension=php_fileinfo.dll

or

php_admin_flag extension=php_fileinfo.dll

#2


0  

If you only want to upload images then it might help you.

如果您只想上传图片,那么它可能对您有所帮助。

$image_size_info = @getimagesize($filename); //surpress errors(@) if not image
if( empty($image_size_info) ) $mime_type = "";
else $mime_type = @image_type_to_mime_type( $image_size_info[2] );

//safety for all cases:
if( empty($mime_type) ) $mime_type = "";

if(  strpos($mime_type, "image/") === false  )
{
//not an Image !
} 
else 
{
  //proceed file upload
}

If above code doen't work then it might help you -->> https://github.com/thephpleague/flysystem/commit/0ec96b1104e57bfcf0e40d6169c8e203b7942b34

如果上面的代码不起作用,那么它可能对你有帮助 - >> https://github.com/thephpleague/flysystem/commit/0ec96b1104e57bfcf0e40d6169c8e203b7942b34

#3


0  

If you have cpanel just go to

如果你有cpanel就去

Select PHP Version

选择PHP版本

and search for the extension

并搜索扩展名

fileinfo

check it and you're good to go.

检查它,你很高兴。