[Solved] Enable/Disable allow_url_fopen from .ini OR .htaccess

Advertisement

The allow_url_fopen is a filesystem and streams configuration option.

It enables the URL-aware fopen() wrappers that enable accessing URL objects like files.

If allow_url_fopen() is enabled then we can access the remote files using the FTP or HTTP protocol.

The extensions like zlib may register additional wrappers.

We are going to see:

What is the issue if allow_url_fopen is disabled? What is the issue if allow_url_fopen is disabled?

Suppose you want to read the file with the PHP function fopen().

E.g.

// index.php
$file = fopen("http://www.example.com/", "r");

If the allow_url_fopen is disabled then you see the error:

E.g.

$ php index.php
allow_url_fopen is DISABLED.
PHP Warning:  fopen(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in C:\xampp\htdocs\tutorials\index.php on line 9
PHP Stack trace:
PHP   1. {main}() C:\xampp\htdocs\tutorials\index.php:0
PHP   2. fopen() C:\xampp\htdocs\tutorials\index.php:9

Warning: fopen(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in C:\xampp\htdocs\tutorials\index.php on line 9

Call Stack:
    0.4036     406560   1. {main}() C:\xampp\htdocs\tutorials\index.php:0
    0.4037     406592   2. fopen() C:\xampp\htdocs\tutorials\index.php:9

Variables in local scope (#1):
  $file = *uninitialized*

PHP Warning:  fopen(http://www.example.com/): failed to open stream: no suitable wrapper could be found in C:\xampp\htdocs\tutorials\index.php on line 9
PHP Stack trace:
PHP   1. {main}() C:\xampp\htdocs\tutorials\index.php:0
PHP   2. fopen() C:\xampp\htdocs\tutorials\index.php:9

Warning: fopen(http://www.example.com/): failed to open stream: no suitable wrapper could be found in C:\xampp\htdocs\tutorials\index.php on line 9

Call Stack:
    0.4036     406560   1. {main}() C:\xampp\htdocs\tutorials\index.php:0
    0.4037     406592   2. fopen() C:\xampp\htdocs\tutorials\index.php:9


Variables in local scope (#1):
  $file = *uninitialize

Error in Composer Update Error in Composer Update

You may see a similar error in the command line something like:

 $ composer update
 Loading composer repositories with package information
 The "https://repo.packagist.org/packages.json" file could not be downloaded: allow_url_fopen must be enabled in php.ini (https:// wrapper is disabled in the server configuration by allow_url_fopen=0
 failed to open stream: no suitable wrapper could be found)
 https://repo.packagist.org could not be fully loaded, package information was loaded from the local cache and may be out of date
 Updating dependencies (including require-dev)
 [Composer\Downloader\TransportException]
   The "http://repo.packagist.org/p/scribu/lib-posts-to-posts%24375806a5270591820170d67f4467d3f709c9e02e790b81753f82c8c15ea5cebd.json" file could not be downloaded: allow_url_fopen must be
   enabled in php.ini (http:// wrapper is disabled in the server configuration by allow_url_fopen=0
   failed to open stream: no suitable wrapper could be found)
 update [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--lock] [--no-custom-installers] [--no-autoloader] [--no-scripts] [--no-progress] [--no-suggest] [--with-dependencies] [--with-all-dependencies] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [-i|--interactive] [--root-reqs] [--] []…

Both errors are the same and point to the allow_url_fopen option.

Top ↑

Check the allow_url_fopen is enabled or disabled Check the allow_url_fopen is enabled or disabled

We see how we get an error if the allow_url_fopen is disabled.

Now, Let’s see how to check whether the allow_url_fopen is Enabled or Disabled on the server.

To check it, PHP gives us the function ini_get().

The ini_get() allows us to get the value of our server configuration option.

We can use it as:

ini_get( 'allow_url_fopen' )

Top ↑

Enable the allow_url_fopen Enable the allow_url_fopen

We have two ways to enable the allow_url_fopen on the server.

Top ↑

Enable the allow_url_fopen with .ini file Enable the allow_url_fopen with .ini file

  • Open the php.ini file
  • Search for allow_url_fopen

If you find it as:

allow_url_fopen = Off

then, replace Off with On text as:

allow_url_fopen = On

(NOTE: If you have not found the allow_url_fopen then you simply add the same code as allow_url_fopen=On which enable it.)

How to confirm whether the allow_url_fopen is enabled or not?

You can check it with the condition ini_get( 'allow_url_fopen' ).

E.g.

// index.php
if ( ini_get( 'allow_url_fopen' ) ) {
     echo 'allow_url_fopen is Enabled';
} else {
     echo 'allow_url_fopen is Disabled';
}

Now execute the file in command prompt as:

$ php index.php
allow_url_fopen is ENABLED.

Top ↑

Enable the allow_url_fopen with .htaccess file Enable the allow_url_fopen with .htaccess file

Same as the .ini file you can set the value in the .htaccess file as:

php_value allow_url_fopen On

How to confirm?

Do the same as the above example.

Top ↑

Disable the allow_url_fopen Disable the allow_url_fopen

We have two ways to disable the allow_url_fopen on the server.

Top ↑

Disable the allow_url_fopen with .ini file Disable the allow_url_fopen with .ini file

allow_url_fopen = Off

Top ↑

Disable the allow_url_fopen with .htaccess file Disable the allow_url_fopen with .htaccess file

php_value allow_url_fopen Off

php_value allow_url_fopen On

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: