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
PHP Fatal Error: Call to a Member Function Format() on Boolean
PHP How to Remove Last Part of a Path
Which Is Faster? Constants, Variables or Variable Arrays
Laravel SQLstate[Hy000] [2002] Connection Refused
File_Get_Contents Returns Empty String
Sending/Displaying a Base64 Encoded Image
Auto Increment Skipping Numbers
How to Pass an Array of Pdo Parameters Yet Still Specify Their Types
Problems with German Umlauts in PHP JSON_Encode
Convert PHP Array String into an Array
How to Use PHP to Connect to SQL Server
PHP Copying Array Elements by Value, Not by Reference
Using Return in Ternary Operator
Php's Preg_Replace Regex That Matches Multiple Lines