Link to home
Start Free TrialLog in
Avatar of thenelson
thenelson

asked on

php connect() failed error

I have been using a php for five years with no problems. My webservice (Godaddy) migrated my shared account to a new service and the script is failing on the file_exists()  and fopen() lines below. I have talked with Godaddy support. They have looked into the problem and tell me the problem is with my script and not server side (which makes no sense to me since the problem started with a change in the server and no change in the script). I also ran the lines in a php debugger with no problem.

Here are the script lines (username and password removed for security):
		//create text file
		$filename = "test.txt";
		$pathname= "ftp://username:password@nosuffering.com/barnwellmd/secure/".$filename;
		if(file_exists($pathname)) unlink ($pathname);
		$filehandle= fopen($pathname, "w");
		fwrite($filehandle, $filetext); //write into a file
		fclose($filehandle);

Open in new window

Here are the errors:
Warning: file_exists() [function.file-exists]: connect() failed: Connection refused in /home/content/57/4477757/html/barnwellmd/message.php on line 106

Warning: fopen() [function.fopen]: connect() failed: Connection refused in /home/content/57/4477757/html/barnwellmd/message.php on line 107

Warning: fopen(ftp://...@nosuffering.com/barnwellmd/secure/test.txt) [function.fopen]: failed to open stream: operation failed in /home/content/57/4477757/html/barnwellmd/message.php on line 107

Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/content/57/4477757/html/barnwellmd/message.php on line 108

Warning: fclose() expects parameter 1 to be resource, boolean given in /home/content/57/4477757/html/barnwellmd/message.php on line 109

Open in new window

Any ideas on how to resolve this?
Thanks
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Looks like the permissions may be in play.  Run phpinfo() on the old and the new servers, and compare the outputs for "safe mode" and "allow_url_fopen" settings.  These are initialization values that GoDaddy should have kept consistent when they moved your server.  In my experience, GoDaddy tech support does not understand even the basic things about PHP, and you may need to "escalate" the issue to a higher level in their organization before they can get their eye on the ball!
Avatar of thenelson
thenelson

ASKER

Ray,
The old server is no longer available to me.

How do I run phpinfo()?
Ray,

I ran phpinfo() and attached the output.
phpinfo-output.txt
$pathname= "ftp://username:password@nosuffering.com/barnwellmd/secure/".$filename;
if(file_exists($pathname)) unlink ($pathname);

Open in new window

This is using the PHP file operations to FTP a file - it is likely that this functionality is disabled in the new environment.

Have you tried the PHP FTP library as an alternative

http://php.net/manual/en/book.ftp.php
Julian,

I tried pasting
ftp://username:password@nosuffering.com/barnwellmd/secure/
into the browser address bar with the correct username:password of course and it opened the folder.

I suppose you are saying the new environment will not support file_exists()  and fopen() to a ftp address.
I will look into the PHP FTP library.  Do you have some quick code that will replace the code I posted?
Couple of things.

This is PHP 5.3, an obsolete release.  There are no updates available any more for PHP 5.3, not even for security fixes.  Tell GoDaddy that you require up-to-date software.  If they cannot provide it -- immediately -- consider moving to another hosting company.  HostGator, LiquidWeb, Pair, and many others can support you with a good platform and good tech support.  I've used ChiHost for many years and never had an experience like this.  The differences in cost between the quality hosting companies and GoDaddy are small, ephemeral, and vanish the first time you find yourself dealing with a hosting-induced emergency like this one.

Ask GoDaddy to run phpinfo() on the old server and furnish the output to you.  Then you can compare the old and the new to look for differences.

You might try adding error_get_last() to the script to visualize information about what is going on.  Be sure you have error_reporting(E_ALL) set, too.

It's unreasonable to believe that you have to rewrite your application just because GoDaddy moved your application to a new server.  I'm almost 100% sure this is GoDaddy's problem, not yours, and they are the ones who should fix it.
Interesting... NoSuffering.com seems to hang trying to connect to abac.com.  

http://nosuffering.com/barnwellmd/ connects and renders a web page.
I suppose you are saying the new environment will not support file_exists()  and fopen() to a ftp address.
Yes - for security reasons this is often turned off. Nothing to do with the server you are connecting to - URL will work from other locations - implies a local issue on the server the script is running on. The fact it worked on the old and not the new to me indicates that the new has security measures in place to prevent this - you will probably find that file_get_contents() fails on a remote URL as well - where it probably worked on the old system
According to PHPinfo() allow_url_fopen is enabled, so the assumption would be that file_get_contents() to a remote URL will work correctly.  Man page references:
http://php.net/manual/en/function.fopen.php
http://php.net/manual/en/function.file-get-contents.php
http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
http://php.net/manual/en/features.remote-files.php

Please try running this script and show us what you get, thanks,
<?php // demo/temp_thenelson.php
/**
 * https://www.experts-exchange.com/questions/28986906/php-connect-failed-error.html
 */
error_reporting(E_ALL);

$url = 'https://iconoun.com/demo/storage/short.txt';
$doc = file_get_contents($url);
var_dump($doc);

Open in new window

According to PHPinfo() allow_url_fopen is enabled, so the assumption would be that
I missed the post with the dump.

To clarify
Old server works
Browser works
So not an IP restriction on the destination side

New server does not work

Connection refused in /home/content/57/4477757/html/barnwellmd/message.php on line 106
Suggests there is more to the script than posted here.

Also found this PAQ that might be relevant https://www.experts-exchange.com/questions/28691055/Permission-denied-errors.html
All of the hosts that I use have disabled FTP thru PHP.  They claim it is a security issue.  And phpinfo() can be misleading.  On one account that I know that FTP thru PHP is disabled, phpinfo() still says it's enabled.

Another issue is that Godaddy blocks a lot of ports on shared hosting.  Ones that I know of are 3306 used to connect to a remote MySQL server and 1433 used to connect to a remote MS SQL server.  They also block port 25 to prevent you from sending email thru a remote server.
Ray,

I ran the script you suggested, got no errors and the browser screen shows:
string(26) "This is a short text file "

I'll leave the script on the server for a while. It's URL is http://barnwellmd.com/test.php

I added
 error_reporting(E_ALL);
 error_get_last() ;
to the script but the output did not change.

Julian,

I tried replacing the lines I posted with:
		$ftp_server = "ftp://nosuffering.com/barnwellmd/secure/";
		$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
		$login = ftp_login($ftp_conn, "username", "password");
		$res = ftp_size($ftp_conn, $filename);

		if ($res != -1) {
			echo "size of $file is $res bytes";
		} else {
			echo "couldn't get the size";
		}

Open in new window

The results I got were:
Warning: ftp_connect() [function.ftp-connect]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/content/57/4477757/html/barnwellmd/message.php on line 105
Could not connect to ftp://nosuffering.com/barnwellmd/secure/

Open in new window

SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ray,

I have been thinking of moving to another hosting company also. I looked at HostGator, LiquidWeb, Pair, and ChiHost. Just looking at pricing, it looks like the HostGator Business Plan might suit me. Have you had experience with them?
What's the permissions of
nosuffering.com/barnwellmd/secure/test.txt
and

/home/content/57/4477757/html/barnwellmd/message.php

?

Maybe when they transferred your files they didn't carry over the permissions.

Chmod the folder and files if needed.
Julian,
I changed the code as you suggested. No errors but I got: "Could not connect to nosuffering.com". Also tried ftp.nosuffering.com

NerdsOfTech,
Permissions for both:User generated image
If you try that script locally does it work - you need to fix the error on line 10 $file => $filename

I tested here against my FTP server - works fine.

Have you tried logging in to a terminal session on the server and doing a manual ftp to the remote host - does that work.
Julian,

>If you try that script locally does it work - you need to fix the error on line 10 $file => $filename
I'm nut sure what you mean. The script is running on Godaddy's server and accesses another folder on the same server, same domain. I don't have a php engine or debugger on my computer.

>I tested here against my FTP server - works fine.
The original code worked fine on the old Godaddy server.

>Have you tried logging in to a terminal session on the server and doing a manual ftp to the remote host - does that work.
Yes and using explorer2 I can connect to the secure folder that fails in the php script using: ftp://username:password@nosuffering.com/barnwellmd/secure/
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ray,

I agree with you. I don't think that scripts that have been working for years should stop working just because the server was changed to a newer server. I think my last support call was escalated as my tech guy was talking to a server executive guy. They wouldn't let me talk to the other guy directly. I had two php scripts break with the migration. The other script uses a third party (Twilio). With Twilio's tech support help, we found a work around for that script. It's probably time to leave them behind. I've started a chat session with HostGator, waiting for them to answer I have 8 domains (4 active), 1 SSL certificate and 4 active email accounts. It looks like their Business Plan should cover everything I need.
Good luck with HostGator.  My experience with them was very positive!
I am assuming this problem will be resolved once I am migrated to a new service.

Thanks to all who commented!!!

BTW: I just uploaded another question: ready to upload mysql secure email utility   https://www.experts-exchange.com/questions/28987096/ready-to-upload-mysql-secure-email-utility.html
and accesses another folder on the same server, same domain
Same domain + same server - why do you need FTP?
Same domain + same server - why do you need FTP?
good point!  once I'm on the new server, I'll change the coding.
Thanks!
If my php script is in a non password protected folder and is saving a file into a password protected folder, do I need to handle that?
Password protected folder?
A follow up:

I set up an account with HostGator. It was a very unpleasant experience. Their website states that they will migrate a website for free and it would take about 3 days. I filled out the request for the migration. After two weeks, I got an email that the migration was not yet started and would cost $75. I complained but they would not budge. Their website states that they will provide a full refund for accounts cancelled within 45 days. I filled out their form to cancel the service and get a refund of my fees. They responded by cancelling the account at the end of the hosting term in 2019. I filed a dispute with my credit card company to get the charge reversed.  I would not recommend Hostgator at all.

I am going to try one of the other webserver providers.
Sorry to hear that.  Best of luck with the others!