- PHP message: Connection refused
- The Cause
- The Solution
- PHP- file_get_contents failed to open stream: Connection refused
- Best Solution
- Related Solutions
- PHP- file_get_contents failed to open stream: Connection refused
- Best Solution
- Related Solutions
- php — File_get_contents(): failed to open stream: Connection refused
- Answer
- Solution:
- Answer
- Solution:
- Share solution ↓
- Additional Information:
- Didn’t find the answer?
- Similar questions
- Write quick answer
- About the technologies asked in this question
- PHP
- CodeIgniter
- HTML
- Welcome to programmierfrage.com
- Get answers to specific questions
- Help Others Solve Their Issues
- Latest questions:
- 984 mysql — Select multiple rows from multiple tables and print all withe php while loop
- 155 javascript — I get the 500 error when I try to return a value from PHP object/array. But it’s ok return all the object
- 16 php — checkbox values not being received in laravel controller
- 167 php — can i use laravel Queues to delete some table records automatically?
- 775 php — How to display the live online user in the system using CodeIgniter?
- 36 php — Echos showing before input is received
- 969 php — How to make nav bar active when it is clicked in laravel 5.6?
- 581 How to continuously read data from different html page into one curly bracket in JSON file using PHP?
- 886 How to get sum of distinct key -> value in php arrays
- 555 php — I am confused with third party login
- Users questions:
- mysql
- Warning: Undefined array key «program» in E:\xampp\htdocs\pi\alumni.php on line 27 Warning: Undefined array key «enter_course» in E:\xampp\htdocs\pi\alumni.php on line 31 Warning: Undefined array key «enter_address» in E:\xampp\htdocs\pi\alumni.php on line 32
- Rename existing images with keyword in WordPress PHP
- codeigniter
- Class «web_profiler.controller.profiler» does not exist after upgrading symfony 3.4 to 4.4 version
PHP message: Connection refused
You log on to a server that is having performance issues, you do some basic troubleshooting only to find a PHP error log, several gigabytes in size, that contains just the one, particularly unhelpful, error message… over and over again.
[Wed Jun 04 13:24:18.154183 2021] [proxy_fcgi:error] [pid 817092:tid 139701387010829] [client 1002:41e0:6:b32a. 0] AH01234: Got error 'PHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refusedPHP message: Connection refused
The Cause
With such a generic error message it could be any number of things but in my case it turned out to be caused by W3 Total Cache for WordPress (W3TC). W3TC supports a number of different cache methods, this includes things like Redis and Memcached. If you have configured W3TC to use Redis and you have the incorrect connection settings or you are on shared hosting and don’t have the appropriate access you might end up with an error log that is spamming the phrase `PHP message: Connection refused` over and over again, dozens or hundreds of times a second.
It should be noted that this won’t stop the WordPress site from working, it might not even have a major performance impact, the first sign of a problem may even be the server running out of disk space.
The Solution
Switch to a disk based cache or reconfigure/get access to the caching service you have selected. Honestly this is pretty obvious once you actually know what the issue is.
Edit: It might also be worth double checking Redis/Memcached is actually installed as W3TC can sometimes show the option as active when it’s not actually installed on the server.
sudo systemctl status redis sudo systemctl status memcached
PHP- file_get_contents failed to open stream: Connection refused
and its working fine here and showing the country code.
But it showing error on Server
$IP=$_SERVER['REMOTE_ADDR']; $country_code = file_get_contents('http://api.hostip.info/country.php?ip=' . $IP);
Warning:
file_get_contents(http://api.hostip.info/country.php?ip=101.63.xx.xxx)
[function.file-get-contents]: failed to open stream: Connection
refused in /srv/disk4/1322145/www/servername.in/app/header.php
on line 12
Best Solution
You can use CURL in place of file_get_contents()
Related Solutions
Php – How to handle the warning of file_get_contents() function in PHP
Step 1: check the return code: if($content === FALSE) < // handle error here. >
Step 2: suppress the warning by putting an error control operator (i.e. @ ) in front of the call to file_get_contents(): $content = @file_get_contents($site);
Php – file_get_contents throws 400 Bad Request error PHP
You might want to try using curl to retrieve the data instead of file_get_contents. curl has better support for error handling:
// make request $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://api.twitter.com/1/statuses/user_timeline/User.json"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); // convert response $output = json_decode($output); // handle error; error output if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) < var_dump($output); >curl_close($ch);
This may give you a better idea why you’re receiving the error. A common error is hitting the rate limit on your server.
PHP- file_get_contents failed to open stream: Connection refused
and its working fine here and showing the country code.
But it showing error on Server
$IP=$_SERVER['REMOTE_ADDR']; $country_code = file_get_contents('http://api.hostip.info/country.php?ip=' . $IP);
Warning:
file_get_contents(http://api.hostip.info/country.php?ip=101.63.xx.xxx)
[function.file-get-contents]: failed to open stream: Connection
refused in /srv/disk4/1322145/www/servername.in/app/header.php
on line 12
Best Solution
You can use CURL in place of file_get_contents()
Related Solutions
Php – How to handle the warning of file_get_contents() function in PHP
Step 1: check the return code: if($content === FALSE) < // handle error here. >
Step 2: suppress the warning by putting an error control operator (i.e. @ ) in front of the call to file_get_contents(): $content = @file_get_contents($site);
Php – file_get_contents throws 400 Bad Request error PHP
You might want to try using curl to retrieve the data instead of file_get_contents. curl has better support for error handling:
// make request $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://api.twitter.com/1/statuses/user_timeline/User.json"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); // convert response $output = json_decode($output); // handle error; error output if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) < var_dump($output); >curl_close($ch);
This may give you a better idea why you’re receiving the error. A common error is hitting the rate limit on your server.
php — File_get_contents(): failed to open stream: Connection refused
I am trying to download a previously uploaded file, form database and uploads folder in php codeigniter. I am using code below but downloading empty file.
public function downloadFile($incidents_id) < $file = $this->incidents_model->getfile($incidents_id); //echo $file; die; $this->load->helper('download'); $path = file_get_contents(base_url()."uploads/".$file); //(the error shows on this line) // echo $path; die; $name = $file; // new name for your file // echo $name ; die; force_download($name, $path); // start download` >
function getfile($Incident_id ) < $file = $this->db->select('file'); $this->db->from('incidents'); $this->db->where('incidents_id' , $Incident_id ); $query = $this->db->get(); // return $query->row(); if ($query->num_rows() > 0) < return $query->row()->file; > return false; >
so running this code download an empty file.
echo $file; die; displays the file name which been saved in db and in uploads folder
echo $path; die; generates an error: Severity: Warning
Message: file_get_contents(http://localhost:8080/ticketing_tool_v2/uploads/Screenshot 2021-03-04 at 5.59.38 PM.png): failed to open stream: Connection refused
Filename: admin/Incidents.php
Line Number: 380
Answer
Solution:
Reviewing the documentation for file_get_contents you’ll observe there’s many different ways you can use it. For your purposes you would need to allow inbound connections to the filesystem.
The other way you could do this for better future proofing is to use the CodeIgniter file helper — https://codeigniter.com/userguide3/helpers/file_helper.html
Answer
Solution:
Before reading the file from path, please check whether a path points to a valid file.
public function downloadFile($incidents_id) < try < $this->load->helper('download'); $file = $this->incidents_model->getfile($incidents_id); $data = file_get_contents(base_url("uploads/" . $file)); $name = $file; // new name for your file force_download($name, $data); // start download` > catch (Exception $e) < //exception handling code goes here print_r($e); >>
Share solution ↓
Additional Information:
Didn’t find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor — hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites. The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
CodeIgniter
CodeIgniter is a framework that is known for requiring a minimum amount of customization to get it up and running. This allows those who choose it to work at a good pace. It has been updated many times since its inception in 2006. Now the most recent version is 4.0.3.
https://www.codeigniter.com/
HTML
HTML (English «hyper text markup language» — hypertext markup language) is a special markup language that is used to create sites on the Internet. Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.
Latest questions:
984 mysql — Select multiple rows from multiple tables and print all withe php while loop
155 javascript — I get the 500 error when I try to return a value from PHP object/array. But it’s ok return all the object
16 php — checkbox values not being received in laravel controller
167 php — can i use laravel Queues to delete some table records automatically?
775 php — How to display the live online user in the system using CodeIgniter?
36 php — Echos showing before input is received
969 php — How to make nav bar active when it is clicked in laravel 5.6?
581 How to continuously read data from different html page into one curly bracket in JSON file using PHP?
886 How to get sum of distinct key -> value in php arrays
555 php — I am confused with third party login
Users questions:
mysql
Warning: Undefined array key «program» in E:\xampp\htdocs\pi\alumni.php on line 27 Warning: Undefined array key «enter_course» in E:\xampp\htdocs\pi\alumni.php on line 31 Warning: Undefined array key «enter_address» in E:\xampp\htdocs\pi\alumni.php on line 32
Rename existing images with keyword in WordPress PHP
codeigniter
Class «web_profiler.controller.profiler» does not exist after upgrading symfony 3.4 to 4.4 version
© 2021-2023 Programming problem solving site for beginners and advanced. Answers to questions related to coding.
This site uses cookies. We use them to improve the performance of our website and your interaction with it. Confirm your consent by clicking OK