PHP Quick Tip: Check if URL aware fopen wrappers are enabled
Posted July 19th, 2010 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:
- Show PHP configuration options for a selected extension (Monday, May 25th 2009)
- PHP's ini_get_all function (Monday, May 18th 2009)
- Automatically append or prepend files in a PHP script (Monday, May 11th 2009)
- Set PHP configuration options with an Apache .htaccess file (Thursday, April 16th 2009)
- PHP list of php.ini directives (Tuesday, December 30th 2008)
- PHP is not showing any error messages (Tuesday, June 10th 2008)

Comments
blog comments powered by Disqus