Skip to content

Setting the allow_url_fopen PHP directive

The allow_url_fopen directive in PHP controls whether functions like file_get_contents(), include(), and require() can access remote files using URL wrappers (e.g., http:// or ftp://). By default, many hosting environments disable this directive for security reasons.

  • Enable: Required by some applications to fetch data from external servers or APIs.
  • Disable: Prevents remote file inclusion vulnerabilities and reduces the risk of arbitrary code injection.

Edit or create a php.ini file in your application directory:

; Enable
allow_url_fopen = On
; Disable
allow_url_fopen = Off

If your hosting environment supports .user.ini, add:

allow_url_fopen = On

or

allow_url_fopen = Off

On Apache servers, you can override the directive:

php_flag allow_url_fopen On
php_flag allow_url_fopen Off
  1. Log in to cPanel.
  2. Go to Software → Select PHP Version → Options tab.
  3. Locate allow_url_fopen.
  4. Toggle between On and Off.
  5. Save changes.
  1. Create a test file info.php:

    <?php phpinfo(); ?>
  2. Load it in your browser and search for allow_url_fopen.

  3. Confirm whether it is enabled or disabled.

  4. Delete the file afterward for security.

  • Keep allow_url_fopen disabled unless your application specifically requires it.
  • Use cURL (curl_init, curl_exec) instead of allow_url_fopen for safer and more flexible remote requests.
  • Always validate and sanitize external data to prevent injection attacks.