views:

44

answers:

2

I recently upgraded my XAMPP from PHP 5.2 to 5.3.1

I seem to be having a problem with file_get_contents().

I can use the function to get something like "http://www.google.com", but it times out when I use it on a domain I have setup locally e.g. "http://localhost/my_dir/my_css_file.css".

I'm not really sure what the problem is. If it's a bug, is there a viable alternative?

Kindly advise.

A: 

Any chance you're on a Windows system? There is a bug in a combination of Windows, file_get_contents and localhost which is not going to be fixed. See http://bugs.php.net/38826 and http://bugs.php.net/40881

Try using 127.0.0.1 instead of localhost or set up any different domain name. Then you should get it working.

sprain
Yes I'm on Windows. I'm actually not using localhost per se. I have a lot of domain names defined in my hosts file, and also in my apache.conf. So I can setup something like http://mydomain/myfiledir/myfile.css for example. Is there a workaround for this?!
Chuck Ugwuh
Is `file()` working?Did you try @Ignatz's solution?
sprain
+2  A: 

Try to use include() instead of file_get_contents().

<?php include($_SERVER['HTTP_HOST'] . "/my_dir/my_css_file.css"); ?>

or

<?php include($_SERVER['DOCUMENT_ROOT'] . "/my_dir/my_css_file.css"); ?>

Updates corresponding your comments:

$string = get_include_contents('somefile.php');

function get_include_contents($filename) {
    if (is_file($filename)) {
        ob_start();
        include $filename;
        $contents = ob_get_contents();
        ob_end_clean();
        return $contents;
    }
    return false;
}

This will get file data into variable $string.

Happy
I need to get this data into a variable that's why file_get_contents is essential. Not sure if I can do the same with include()?
Chuck Ugwuh
@Chuck - Have you checked your file permissions?
webfac
@Chuck Ugwuh - try updated answer.
Happy
@lgnatz - Nice variation on file_get_contents(), I like it.
webfac
This looks like it would work. Unfortunately, I have a separate ob_start() implementation going on with my page. I'm trying to see if I can work something out with CURL.
Chuck Ugwuh