连接到PHP以保护FTP服务器 [英] Connection to secure FTP Server from PHP
问题描述
这个问题符合这个问题,我试图连接到安全的FTP服务器,它无法连接,更奇怪的部分是,我能够做到SSH和连接到服务器,但是当我尝试从PHP代码使用几种不同的方法做到这一点,但它不工作
方法:
- FTP Wrappers
ftp_connect
&ftp_login
-
ftp_ssl_connect
-
ssh2_sftp
-
ssh2-scp-send
&ssh2-scp-receive
- 还没有尝试过这种方法,但在评论部分推荐,所以可以在此工作并稍后发布更新。
方法1的代码:
$ ftp_server =ftp:// username:password@192.168.1.1:21 / {$ log_file_name};
$ opts = array('ftp'=> array('overwrite'=> TRUE));
$ context = stream_context_create($ opts);
$ put_file = file_put_contents($ ftp_server,$ response,LOCK_EX,$ context);
这里也无法连接到安全的FTP服务器,因此无法提供任何建议
$ b
方法2的代码:
c $ c> ftp_server ='www.server.com';$ conn_id = ftp_connect($ ftp_server)或死(无法连接到主机);
//程序在这里消失并给出错误信息无法连接到主机,
//但为什么ftp_login在这里不起作用,任何建议?
$ ftp_user_name =login;
$ ftp_user_pass =密码;
//使用用户名和密码登录
$ login_result = ftp_login($ conn_id,$ ftp_user_name,$ ftp_user_pass);
//检查连接和登录结果
if((!!conn_id)||(!$ login_result))
{echoFTP连接遇到错误!
echo试图连接到$ ftp_server用户$ ftp_user_name ....;
//退出;
} else
{
echo连接到$ ftp_server,对于用户$ ftp_user_name。.....;
}
方法3的代码:
这里使用与方法1相同的代码,而不是ftp_connect,我使用ftp_ssl_connect
方法4代码:
$ connection = ssh2_connect('www.server .com',22);
ssh2_auth_password($ connection,'login','password');
$ sftp = ssh2_sftp($ connection);
// exit();
$ stream = fopen(ssh2.sftp:// $ sftp / path / to / file,'r');
任何人都可以告诉我为什么我无法使用上述方法连接到安全的FTP服务器,但仍然可以如何使用ssh来实现这一点?
是否有其他方法可以使用php连接到安全的FTP服务器?
更新:
Q1。我再次尝试使用ftp_connect,但它刚刚熄灭,为什么它消失, ftp_connect消亡的情况是什么?
Q2。我们是否只有这种连接服务器的方法或者是否有其他方法我们可以实现哪些功能?
Q3。是否这个php语言相关,它不支持安全的FTP连接?或者有任何其他方式使用PHP来做到这一点,如果是的话,比提供不同的方法,因为这将是非常有益的。
更新1:
似乎如果ftp_connect不能工作,防火墙可能是其原因之一。我不完全确定是否属于这种情况,但如果我发现任何有用的信息,我正在对其进行更多研究并在此处发布更新。
可能的解决方案:
$ b
问题 p>
如果我删除了or die,那么您会收到错误消息:
从网页运行时: p>
警告:ftp_login()期望参数1是资源,布尔在28行/var/www/ftp_test.php中给出
var_dump($ conn_id);返回bool(false)。
从命令行/ usr / bin / php /var/www/ftp_test.php
的var_dump($ conn_id);返回类型为(FTP缓冲区)的资源(4)。
脚本完成。
解决方案1
这可能是一个 解决方案$ c>
:
尝试关闭 selinux
,这里是 way
或 Search:如何禁用selinux 将其暂时或永久关闭。
解决方案2
如果您不想完全关闭selinux ,只需使用 setsebool 命令设置httpd_can_network_connect即可获得所需内容。
验证它之前是否设置为关闭:
getsebool httpd_can_network_connect
将其设置为on:
setsebool httpd_can_network_connect = 1
重启selinux:
setenforce 1
确保php ftp_connect
在httpd下运行时仍然有效。
将策略(-P)设置为on,以便在重新启动时持续:
setsebool -P httpd_can_network_connect = 1
解决方案3
也可能有issu e与公司防火墙。确保它已正确配置并且具有正确设置的访问权限。
解决方案4 另外一种方法是使用 cURL:libcurl ,因为它可以用来连接并与许多不同类型的服务器通信的协议 解决方案5 有一个名为 PHP安全通信库(phpspeclib),它也可用于建立到FTP服务器的安全连接。 至于'ftp通过ssh'别名'ftp':没有直接的建议,但是请注意,专用服务器的虚拟主机不支持它(这很糟糕)。公司防火墙可能会阻止相关端口。 至于'ftp使用ssl / tls的别名'ftps': 你的答案在这里。不要浪费时间在 ,至少可以说) This question is in line with this question, I am trying to connect to secure FTP Server and it is not able to connect, wierd part is that I am able to do ssh and connect to the server but when I try to do it from php code using few different approaches but it is not working Approaches: Code for Approach 1: Here also am not able to connect to secure FTP Server, any suggestions as to why it is not able to connect ? Code for Approach 2: Code for Approach 3: Code for Approach 4: Can anyone advise why am I not able to connect to secure FTP Server using above approaches but still am able to do so using ssh ? Are there any other approaches to connect to secure FTP Server using php ? UPDATE: Q1. I tried again using ftp_connect but it just died out and why does it dies out, what are the scenarios in which ftp_connect dies out ? Q2. Do we have only this approaches to connect to the server or are there any other which we can implement ? Q3. Is this php language related that it does not support secure FTP Connection ? OR there is any other way of doing this using php, if yes than do provide different approaches as it would be very helpful. UPDATE 1: I was trying to google more on the issue and it seems that if ftp_connect does not work than firewall could be one of the reason for it. I am not totally sure if that is the case but I am researching more on it and post an update in here if I find anything useful. Possible Solution : Problem If I remove the "or die" then you get the error: When running from a webpage: Warning: ftp_login() expects parameter 1 to be resource, boolean given in /var/www/ftp_test.php on line 28 var_dump($conn_id); returns bool(false). From command line /usr/bin/php /var/www/ftp_test.php var_dump($conn_id); returns resource(4) of type (FTP Buffer). Script completes. Solution 1 This could be one Try to turn off Solution 2 If you don't want to turn off selinux completely, you might get what you need by just setting the httpd_can_network_connect using the setsebool command. Verify that it was previously set to "off":
getsebool httpd_can_network_connect Set it to "on": Turn selinux back on:
setenforce 1 Check to be sure php Set the policy (-P) to "on" so it persists over a reboot: Solution 3 There could also be issue with company firewall. Make sure it is configured properly and has access rights set properly. Solution 4 Another approach is to use cURL : libcurl as it can be used to connect and communicate to many different types of servers with many different types of protocols Solution 5 There is open source project called PHP Secure Communication Library (phpspeclib) which can be also used to establish secure connection to FTP Server. As for ´ftp via ssh´ alias ´sftp´: No direct advice, but note, that many cheap 'non-dedicated server' webhosts do not support it (which is bad). Company firewalls might block the relevant ports. As for 'ftp using ssl/tls' alias 'ftps': Your answer is here. Don't waste time on (Yes, it's poorly documented on the php site, to say the least) 这篇关于连接到PHP以保护FTP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
ftp_ssl_connect()
: - )
ftp_connect
& ftp_login
ftp_ssl_connect
ssh2_sftp
ssh2-scp-send
& ssh2-scp-receive
-- Have not tried this approach yet but recommended in comments portion and so would work on this and will post updates later. $ftp_server = "ftp://username:password@192.168.1.1:21/{$log_file_name}";
$opts = array('ftp' => array('overwrite' => TRUE));
$context = stream_context_create($opts);
$put_file = file_put_contents($ftp_server, $response, LOCK_EX,$context);
ftp_server = 'www.server.com';
$conn_id = ftp_connect($ftp_server) or die ("Cannot connect to host");
//Program dies out here and give error message "Cannot connect to host",
//but why ftp_login does not work here, Any Suggestions ?
$ftp_user_name = "login";
$ftp_user_pass = "password";
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection and login result
if ((!$conn_id) || (!$login_result))
{ echo "FTP connection has encountered an error!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name....";
//exit;
} else
{
echo "Connected to $ftp_server, for user $ftp_user_name".".....";
}
Here am using same code as approach 1 but instead of ftp_connect, am using ftp_ssl_connect
$connection = ssh2_connect('www.server.com', 22);
ssh2_auth_password($connection, 'login', 'password');
$sftp = ssh2_sftp($connection);
//exit();
$stream = fopen("ssh2.sftp://$sftp/path/to/file", 'r');
solution
: selinux
and here is the way
or Search : How to disable selinux for turning it off temporarily or permanently. setsebool httpd_can_network_connect=1
ftp_connect
still works when running under httpd.setsebool -P httpd_can_network_connect=1
ftp_ssl_connect()
:-)