PHP Warning: file_get_contents failed to open stream: Connection refused in script.php

Sharing is caring!

You can create a temp file in your document root folder and after the tests you will need to delete it so you don’t expose extra sever info to always curious spammers/hackers.

if you get this php error you may need to check your php.ini settings to ensure that the allow_url_fopen ini setting in the PHP configuration is enabled. This setting is crucial because it controls whether PHP can access files through network protocols like HTTP or FTP when using file-related functions.


If you need to make external requests it’s better to use php curl options as you can confugure it more easily. For example to set timeouts and send the data in the format that the receiving app needs.

If you’re using WordPress you can also use wp_remote_get() or other wp_remote_post() function to make the external calls because you can benefit from the WordPress actions and hooks that you can use to modify the data before or after they are received.

if you have access to the php.ini file from the command line or via the control panel’s php configuration settings do change the value to this one.

allow_url_fopen = On

You can also try adding this to your .htaccess file. Backup first as always!
Do noticed that when adding values to .htaccess there’s no equal (=) sign between the key and the value. It seems space is enough.

php_vaue allow_url_fopen On

if you need to check somewhere in the code if allow_url_fopen is enabled you can use this php code snippet.

if (ini_get('allow_url_fopen')) {
    echo "allow_url_fopen is enabled.";
} else {
    echo "allow_url_fopen is disabled.";

Keep in mind that using allow_url_fopen can pose a security risk, especially if your application constructs file paths or URLs based on user input. Sanitize and validate all inputs to mitigate security risks.

Sharing is caring!

Leave a Comment

Your email address will not be published. Required fields are marked *