I work with 2 servers one is my production server other is my resource server.
I cannot connect to my resource server from my production server over ftp.
I can connect to other servers from my production server.
I can also connect to my resource server from my localhost or filezilla.
I use this code to connect :
$conn_id = ftp_connect("resource server ip", "21", "5");
if ($conn_id) {
echo "connected";
ftp_close($conn_id);
}
print_r(error_get_last());
I don't get any output when I run this script on server(no error).
On localhost it runs no problem.
What can be the problem with this? Is this something that server admin has to resolve? Thanks for help.
You should first check from commandline, whether it's a networking/OS issue or not.
So if you've got shell access to the production server try connecting to the resource server via the commandline ftp client.
If that does not work, you've got a network / firewall / access control problem, not related to php or your software, and you should talk to the sysadmin.
If it does work, then the problem is in your stuff, and you should set the log levels to high, and run this script from commandline, also check the logs of php, php-error, syslog and the resource servers ftp access log and syslog too.
Note: ftp is a not-too-exact beast, the servers and clients have a lot of workarounds built in to treat each other in a way, that works somehow. There could be issues from active (multiple back-and-forth connections) and passive mode (it's like http), also with ls formats and timestamps, timezones and ports.
Also some servers only support ftps (ftp with ssl) - which is not the same as sftp (file transfer over ssh - port 22).
Your production server probably has some firewall rules, and your connection get caught on that, to debug this, please use the commandline ftp client, and/or nmap / netcat.
Related
$con=mysqli_connect('localhost:3306','Punya','password','saain');
My PHP website, which is live is unable to connect to database. It works fine on my Pcs local server. When I write localhost:3306, it executes the echo statement I wrote for debugging.
If I write just localhost, it gives:
http 500 error
.
Cannot say much without knowing the actual error. But I could assume a few things based on what you have given.
When you pass localhost as the hostname to the mysqli_connect() function, it tries to create the connection using a Unix socket instead of TCP/IP. So it seems like either you don't have permissions to access the socket, or you need to get the correct socket information from your hosting provider.
Also, when you pass it as localhost:3306, you force it to connect via TCP/IP instead of socket. That's why it works.
However, you can force mysqli_connect() function to connect via TCP/IP by providing the hostname as 127.0.0.1.
$con = mysqli_connect('127.0.0.1','Punya','password','saain');
If this doesn't work, I assume they have defined a different default port for mysql in the php configuration. You can see it by printing it like:
echo ini_get("mysqli.default_port");
In case this default port is different from 3306 (which is unlikely), you can pass the port as the fifth paramter to mysqli_connect() function like this.
$con = mysqli_connect('127.0.0.1','Punya','password','saain', 3306);
You need to contact your hosting provider to get more details on this. Until you do that, your solution is to connect as shown above.
I hope you understand why it happens :) Feel free to ask if you have any doubts.
Try to check in
check here
Synax errors
check in your 👇
file.php
Check what version your live server is running and what version our location machine is running. most of the time environmental factors make these types of errors. I'm going to assume your hosting environment is a shared hosting for this answer
There are couple of things you can do identify the problem first.
Enable Errors
if you are hosted on a cPanel shared hosting server I doubt you can change the php.ini file. what you can do is enable PHP errors on runtime so you will have a proper error than just a 500 error. put the below codes in the top of your PHP file
<?php
error_reporting(E_ALL);
ini_set("display_errors", "On");
PHP info file
create a php.info file to identify what sort environment you are hosting your website on.
<?php
phpinfo();
?>
Cpanel has there own MYSQL server domain and port. this information you should get when you are creating a database in cPanel.
Once you have the error. paste it here we might be able to give you a better answer how to fix it
I want to connect to an FTP using PHP to upload the reports generated. As per the remote server, the FTP needs to be in ACTIVE mode.
this is my code:
ini_set('display_errors', '1');
error_reporting(E_ALL);
$conn_id = ftp_connect('myftpserver.com', 21);
if($conn_id)
{
// login with username and password
$login_result = ftp_login($conn_id, 'mysuer', 'password');
$passive = ftp_pasv($conn_id,FALSE);
echo "is active?<br/>";
var_dump($passive);
echo 'Login Result:';
var_dump($login_result);
$files_list = ftp_nlist($conn_id, '/MyFolder/');
echo "<br/>files list ";
var_dump($files_list);
}
else
{
var_dump('Unable to connect to FTP Server');
}
When I am running it from the local machine or a normal shared server, I am able fetch the list, but I am unable to run the script from my AWS AMI instance. For testing purpose, I have even opened all inbound traffic too. Still no luck. Also, the point is that if I try with some other ftp details, I am able to get the response of ftp_nlist. But not for this one. I tried it on 3 AWS instances till yet. Yielded the same result.
All I can say is that this is somewhere the issue at my server security group/firewall. But unable to figure it out. Please help.
The response I get from the server:
is active
bool(true)
Login Result:bool(true)
files list bool(false)
Using FTP Active Mode is problematic with AWS Security Groups.
For active mode to work, you will have to open all inbound ports above 1023. If your client supports restricting the range, do so. You will also need to open both port 20 and port 21 inbound and outbound.
The problem is that the FTP client selects a port that it will listen on. Then the FTP client informs the FTP Server of this port number. The FTP Server then connects to this port. This goes against normal AWS Security Group designs meaning only allow specific ports to be open. You can verify this by opening all ports temporarily, test your FTP client and then closing all the ports.
Active Mode is not secure for the FTP client. Passive Mode is not secure for the FTP server (but the better choice).
NOTE: Rotate your FTP credentials often. Your login and password is sent in the clear and are not encrypted.
FTP is a legacy technology, which is still very popular, that should be stored away in the attic.
I am trying to connect to a remote SFTP server using PHP. My code works fine when I connect to a local SFTP account but it times out for remote host. I have made sure through FTP client that host information is correct and its connecting fine.
I am using phpseclib library and my three line code is below.
require_once("phpseclib/Net/SFTP.php");
$sftp = new Net_SFTP('remote_host_IP');
var_dump($sftp->login('<username>', '<password>'));
It returns false (meaning not connected).
What I have done
I have whitelisted script in mod_security just in case its blocking that.
I have tried same script on my local computer and it connects successfully to remote SFTP.
Any valuable hint please?
Do define('NET_SSH2_LOGGING', 2) before initializing Net_SFTP and then do $sftp->getLog() after $sftp->login()
That'll provide enough info with which a diagnostic can be made.
This might help others. You need to make sure TCP_OUT port is open on your remote server to make it work.
Thanks for the help!
We send some files across to a third party with a PHP cron job via FTP.
However sometimes we get the following error:
ErrorException [ 2 ]: ftp_put(): php_connect_nonb() failed: Operation
now in progress (115) ~ MODPATH/fileop/classes/Drivers/Fileop/Ftp.php [ 37 ]
When I say "sometimes" I mean exactly that; most times it goes across fine but about 1 in 5 times we get that error. It's not to do with the files themselves, because they will go happily if we try again.
We've found similar issues online - relating to a bug in PHP with NAT devices or to do with firewall configuration but again the implication is that if this were the case it would never work.
So, why would this work some times and not others?
ftp_set_option($ftpconn, FTP_USEPASVADDRESS, false);
This line of code before setting passivity of the connection ftp_pasv($ftpconn, true);
Solved my problem
FTP(S) uses random ports to set up data connections; an intermittent success rate indicates that not all ports are allowed by a firewall on the client and/or server machines. The port range for incoming (PASV) data connections can be set in the FTP server.
This page has a nice summary:
The easy way is to simply allow FTP servers and clients unlimited
access through your firewall, but if you like to limit their access to
"known" ports, you have to understand the 4 different scenarios.
1) The FTP server should be allowed to accept TCP connections to port
21, and to make TCP connections from port 20 to any (remote ephemeral)
port.
2) The FTP server should be allowed to accept TCP connections to port
21, AND to accept TCP connections to any ephemeral port as well!
3) The FTP client should be allowed to make TCP connections to port
21, and to accept TCP connections from port 20 to any ephemeral port.
4) The FTP client should be allowed to make TCP connections to port
21, and to make TCP connections to any other (remote ephemeral) port
as well!
So, I'm writing this answer after doing some investigation on my FTP server and reading the link you provided elitehosts.com.
I'm using FileZilla FTP server, and there is a specific setting that I had to enter to make it work. Going into the server settings, there is an area titled "Passive mode settings". In that dialog, there is an area titled "IPv4 specific", and within that area there is a setting labeled "External Server IP Address for passive mode transfers:". It's a radio button selection set, and it was on "Default", but since the FTP server is NAT'ed, I changed that radio selection from "Default" to "Use the following IP:" and entered in the external-facing IP address of my gateway provided by my ISP.
After I set this up, it worked! Not terribly sure if your FTP server is NAT'ed, but I thought I would provide the answer on this thread because it seems related.
In addition to Cees answer, I am running vsftp on ec2 and had to comment out the listen_ipv6=YES, listen=YES then "service vsftpd restart".
Although documentation says it will listen on ipv4 as well it wasn't and this resolved the issue.
For me all I had to do was to remove the ftp_pasv( $ftpconn, true ); and everything worked perfectly. I'm not yet sure why but I am trying to find out and I will surely come back when I do get the reason behind it.
This should be a comment under jj_dev2 comment, but I cannot add one due to reputation. But maybe it will be helpful for someone, so I post it here.
We had the same issue as described in the original post. In our case it worked with many customers - except one.
The solution in jj_dev2 comment did work for us. So we investigated what does ftp_set_option($conn, FTP_USEPASVADDRESS, false) actually do. And based on that we found out that in fact customer's FTPS server was configured incorrectly.
In response to PASV command (ftp_pasv($conn, true)) FTP server returns an IP address which the PHP FTP client then will use for data transfers. In our case the FTP server was returning an internal IP address and not the public IP address that we connect to. Customer had to fix their FTP server settings so FTP server would send external IP address in the PASV command response.
(this question might not belong here, but on ServerFault, I don't know exactly where the problem comes from)
I'm trying to connect to an FTP server like this:
$con = ftp_connect( '86.xxx.xx.xxx', 21 ) or die("FTP connect error");
and it always throws me the error message.
I can connect like this to other FTP servers, but not this one. I can also connect to this one via an FTP client.
What server settings could be wrong that permit a client connect but not PHP?
Thanks for any help!
It seems my host was blocking remote FTP access to other servers. Oddly enough it allowed me to connect to my other servers at this host.