Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fopen is not working on my server

Tags:

php

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.

like image 533
Fischer Avatar asked Jan 27 '12 19:01

Fischer


People also ask

Why fopen is not working in PHP?

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.

What does fopen () function do in PHP?

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.

What is fopen URL?

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.

What is fopen return PHP?

The fopen() function opens a file or URL. If the function fails, it returns FALSE and an error on failure.


3 Answers

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.

like image 116
Cheery Avatar answered Oct 13 '22 20:10

Cheery


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.

like image 37
Charles Avatar answered Oct 13 '22 22:10

Charles


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

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

like image 41
lorenzo-s Avatar answered Oct 13 '22 20:10

lorenzo-s