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.
If you have enabled open_basedir further restrictions may apply. If PHP has decided that filename specifies a registered protocol, and that protocol is registered as a network URL, PHP will check to make sure that allow_url_fopen is enabled. If it is switched off, PHP will emit a warning and the fopen call will fail.
The fopen() function opens a file or URL. Note: When writing to a text file, be sure to use the correct line-ending character! Unix systems use \n, Windows systems use \r\n, and Macintosh systems use \r as the line ending character.
The allow_url_fopen is a setting managed through the PHP Options which allows PHP file functions to retrieve data from remote locations over FTP or HTTP. This option is a great security risk, thus do not turn it on without necessity. While do not allow direct changes to PHP. ini on our servers.
The fopen() function opens a file or URL. If the function fails, it returns FALSE and an error on failure.
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.
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.
Search for allow-url-fopen
in your PHP.ini.
http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With