Fischer
Fischer

Reputation: 1523

fopen is not working on my server

It works perfectly on localhost with Xampp, but when I try it on my server (host-ed.net), fopen can't open any url, it only works for files without http.

<?php
$file="http://www.google.es";
$gestor = fopen($file, "r") or die ("Nothing");
?>

In my server with this code shows Nothing. Can be anything on the php.ini ?

EDIT: In the php.ini: allow_url_fopen = On

EDIT: It's solved. My server had it disabled, but now it's enabled. Thanks for the answers.

Upvotes: 10

Views: 38595

Answers (6)

marcovtwout
marcovtwout

Reputation: 5269

If you are using PHP-FPM, also check the configuration files in pool.d/. PHP-FPM pools can override php settings by using php_admin_flag, php_admin_value, php_flag or php_value. (We encountered this on a Ubuntu 16 vps)

Upvotes: 0

Cheery
Cheery

Reputation: 16214

Do you have a free hosting account? Most hosting services limit outgoing connection for free accounts to prevent abuse. Try this:

$fp = fsockopen("some.alive.site", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    echo "Everything is OK";
    fclose($fp);
}

If you'll see an error message - you are not allowed to make outgoing connections.

Upvotes: 1

Dave
Dave

Reputation: 11162

As http://php.net/manual/en/function.fopen.php states,

If filename is of the form "scheme://...", it is assumed to be a URL and PHP will search for a protocol handler (also known as a wrapper) for that scheme. If no wrappers for that protocol are registered, PHP will emit a notice to help you track potential problems in your script and then continue as though filename specifies a regular file.

I would therefore assume that there is no registered wrapped for url's, and php is treating that as a regular filename, which doesn't exist.

Upvotes: 0

Asaph
Asaph

Reputation: 162801

The permission for opening urls with fopen is controlled by the php.ini setting allow_url_fopen. You can see your current setting using phpinfo() which will dump an html doc containing all your server settings.

Upvotes: 3

lorenzo-s
lorenzo-s

Reputation: 17010

Search for allow-url-fopen in your PHP.ini.

http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

Upvotes: 4

Charles
Charles

Reputation: 51411

fopen can't open any url

Check the value of the allow_url_fopen php.ini setting:

var_dump(ini_get('allow_url_fopen'));

It's probably false. You'll need to speak to your web host, or try another method. Mabye CURL is enabled?

You should also check your error_reporting and display_errors values. PHP should have complained loudly about being unable to open the URL. You'll want to turn both of them all the way up while developing code, so you can understand what goes wrong easier.

Upvotes: 13

Related Questions