PHP Quick Tip: Check if URL aware fopen wrappers are enabled

Posted in PHP and Quick Tips -

PHP supports opening remote URLs (e.g. http://www.example.com/something.htmnl) using the fopen and similar functions. It is possible to disable this function in the PHP configuration so this quick tip shows how to check if it is possible to open remote URLs.

The configuration option

The configuration option that enables or disables opening remote URLs is "allow_url_fopen" and will appear in the configuration file like this:

allow_url_fopen = On

Note that this setting can only be set in the main php.ini configuration file so if your hosting provider has disabled it then you will be able to open remote URLs with fopen etc.

Test if it is enabled

To test if opening remote URLs is enabled do this:

if( ini_get('allow_url_fopen') ) {
    // it's enabled, so do something
}
else {
    // it's not enabled, so do something else
}

If opening remote URLs is not enabled, then you will need to enable this option in the main php.ini configuration or see if your hosting provider will do this for you. Otherwise you will need to use CURL to download files, assuming that CURL is available.



Related posts:


Comments