How to enable php_fileinfo extension in PHP?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Enabling PHP_Fileinfo Extension in Various Configurations Introduction: The php_fileinfo extension provides an efficient and easy way to guess the MIME type of files by analyzing their content internally. It's often used in Laravel applications for handling file uploads and image processing. In this post, we will explore how you can enable the php_fileinfo extension even without accessing your remote server's php.ini file or using the ini_set() method. 1. Enabling PHP_Fileinfo Extension via Local php.ini File: If you have access to your local php.ini file, this is the simplest way to enable the php_fileinfo extension. Open the file and search for "extension=php_fileinfo.so" or "extension=php_fileinfo.dll", depending on your PHP installation (Windows uses '.dll' while Linux and macOS typically use '.so'). If it exists, make sure that the line is uncommented and unmodified by removing any leading semicolon to enable the extension. Save the file and restart your web server for the changes to take effect. 2. Enabling PHP_Fileinfo Extension via .htaccess File: The .htaccess file can be used to override certain PHP settings, including the php_fileinfo extension. To add this extension in your existing Laravel application's .htaccess file, follow these steps: 1. Create a new folder named 'php-extensions'. 2. Inside the newly created 'php-extensions' folder, create two files called 'php-gettext.ini' and 'php_fileinfo.ini' with the following contents: - php-gettext.ini: [PHPGetText]
extension=gettext.so
- php_fileinfo.ini: [PHPFileInfo]
extension=php_fileinfo.so
3. Add the following lines to your 'public/.htaccess' file:

          Options +FollowSymLinks 
          RewriteEngine On
          
          # Enable PHP extensions
          RewriteRule ^/php-extensions/(.*).ini$ - [L]
          AddType application/x-httpd-php7 .php
          AddHandler application/x-httpd-php7 .phtml
        
This approach allows you to maintain your existing Laravel app and php.ini file without making any changes, while still enabling the required extensions. Simply restart your web server for the changes to take effect. 3. Enabling PHP_Fileinfo Extension on Shared Hosting Servers: Some shared hosting providers may not allow you to access or modify the php.ini file directly. In this case, you can try installing a custom php-fpm with the additional extensions required for your project. Here are the steps to do so: 1. Create a new folder called 'laravel-custom-php' and put these files inside it (download from https://github.com/gabrielwolynski/laravel-custom-php): - .htaccess - composer.json - index.html - php-gettext.ini - php_fileinfo.ini - php.ini - public 2. Ensure that your web server can access the newly created folder by adding it to your .htaccess file:

          RewriteEngine On
          
          # Enable PHP extensions
          RewriteRule ^/custom-php/(.*).ini$ - [L]
          AddType application/x-httpd-php7 .php
          AddHandler application/x-httpd-php7 .phtml
        
3. Upload the 'laravel-custom-php' folder to your web server, along with your Laravel project. Restart the web server to enable the new PHP configuration. Conclusion: Enabling the php_fileinfo extension can help resolve issues related to image validation in your Laravel application. By following one of these methods, you can ensure that your projects function correctly without compromising any existing settings or configurations. Remember to always check the error logs for further insights into possible issues and consult with a hosting provider if necessary.