Main menu:

Site search

Categories

Archive

Error with file_get_contents(), is there a work around?

Alternative for file_get_contents()

I was trying to use file_get_contents to work the other day and was getting an error message. Turns out the function was disabled in the php config. There is a way around this which I found on wiki.dreamhost.com. see below for an example.

Instead of:

< ?php
$file_contents = file_get_contents('http://example.com/');

// display file
echo $file_contents;
?>

Use this:

< ?php
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://example.com');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);

// display file
echo $file_contents;
?>

Write a comment