Obinwanne Hill Obinwanne Hill - 1 year ago 83
PHP Question

file_get_contents not working for local files

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 "", 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.

Answer Source

Solved this by using CURL. Here's the code. It will work with remote files e.g.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, ''.$file_path_str.'');
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_USERAGENT, sprintf("Mozilla/%d.0",rand(4,5)));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$curl_response_res = curl_exec ($ch);
curl_close ($ch);

I could not use @James solution because I'm using ob_start and ob_flush elsewhere in my code, so that would have messed things up for me.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download