PHP File_Get_Contents Does Not Work on Localhost

PHP file_get_contents does not work on localhost

From the documentation for file_get_contents:

A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

Check in your php.ini so allow_url_fopen is set to on.

EDIT:

I didn't noticed that you actually could use file_get_contents locally, so now I'm thinking that this could have something to do with your firewall settings.

Also, try to set the user_agent in your php.ini if not already done.

file_get_contents not working for local files

Solved this by using CURL. Here's the code. It will work with remote files e.g.
http://yourdomain.com/file.ext

$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.

file_get_contents() working on localhost but not working on online server

Your hosting server typically does NOT have the same folder hierarchy for user home directories as your local server.

Run php_info() on your hosting server to find out what the correct path is.
Look for DOCUMENT_ROOT in the Environment section.

A simple phpinfo.php file looks like this:

<?php
phpinfo();
?>

Issue in file_get_contents(), it is working on the localhost but not working on the host server

On Host server fopen is disabled that's why file_get_contents() function is not working on server . You can use the Curl to get the data .

To check the fopen create the phpinfo(); in test PHP and run test.php file

Thanks



Related Topics



Leave a reply



Submit