从 PHP 连接到安全的 FTP 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2172289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:20:52  来源:igfitidea点击:

Connection to secure FTP Server from PHP

phpftpsftp

提问by user260204

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

这个问题与这个问题一致,我正在尝试连接到安全的 FTP 服务器,但它无法连接,奇怪的部分是我能够执行 ssh 并连接到服务器,但是当我尝试从 php 执行此操作时使用几种不同方法的代码,但它不起作用

Approaches:

方法:

  1. FTP Wrappers
  2. ftp_connect& ftp_login
  3. ftp_ssl_connect
  4. ssh2_sftp
  5. 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.
  1. FTP 包装器
  2. ftp_connect& ftp_login
  3. ftp_ssl_connect
  4. ssh2_sftp
  5. ssh2-scp-send& ssh2-scp-receive-- 还没有尝试过这种方法,但在评论部分推荐,所以会在这方面工作,稍后会发布更新。

Code for Approach 1:

方法 1 的代码:

$ftp_server = "ftp://username:[email protected]: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);

Here also am not able to connect to secure FTP Server, any suggestions as to why it is not able to connect ?

这里也无法连接到安全的 FTP 服务器,关于为什么无法连接的任何建议?

Code for Approach 2:

方法 2 的代码:

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".".....";
}

Code for Approach 3:

方法 3 的代码:

Here am using same code as approach 1 but instead of ftp_connect, am using ftp_ssl_connect

Here am using same code as approach 1 but instead of ftp_connect, am using ftp_ssl_connect

Code for Approach 4:

方法 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');

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 ?

谁能告诉我为什么我无法使用上述方法连接到安全的 FTP 服务器,但仍然可以使用 ssh 连接?

Are there any other approaches to connect to secure FTP Server using php ?

还有其他方法可以使用 php 连接到安全的 FTP 服务器吗?

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 ?

一季度。我再次尝试使用 ftp_connect 但它刚刚消失了,为什么它消失了,ftp_connect 消失的场景是什么?

Q2.Do we have only this approaches to connect to the server or are there any other which we can implement ?

Q2。我们是否只有这种连接到服务器的方法,或者还有其他我们可以实现的方法吗?

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.

Q3。这种 php 语言是否与它不支持安全的 FTP 连接有关?或者有任何其他方法可以使用 php 来做到这一点,如果是的话,那么请提供不同的方法,因为它会非常有帮助。

UPDATE 1:

更新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.

我试图在这个问题上搜索更多信息,似乎如果 ftp_connect 不起作用,那么防火墙可能是原因之一。我不完全确定是否是这种情况,但我正在对其进行更多研究,如果发现任何有用的信息,我会在此处发布更新。

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

警告:ftp_login() 期望参数 1 是资源,布尔值在第 28 行的 /var/www/ftp_test.php 中给出

var_dump($conn_id); returns bool(false).

var_dump($conn_id); 返回布尔(假)。

From command line /usr/bin/php /var/www/ftp_test.php

从命令行 /usr/bin/php /var/www/ftp_test.php

var_dump($conn_id); returns resource(4) of type (FTP Buffer).

var_dump($conn_id); 返回类型(FTP 缓冲区)的资源(4)。

Script completes.

脚本完成。

Solution 1

解决方案1

This could be one solution:

这可能是一个solution

Try to turn off selinuxand here is the wayor Search : How to disable selinuxfor turning it off temporarily or permanently.

尝试关闭selinux,这里是wayor搜索:如何禁用 selinux以暂时或永久关闭它。

Solution 2

解决方案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 setseboolcommand.

如果您不想完全关闭 selinux,您可以通过使用setsebool命令设置 httpd_can_network_connect 来获得所需的内容

Verify that it was previously set to "off": getseboolhttpd_can_network_connect

验证它之前是否设置为“关闭”: getseboolhttpd_can_network_connect

Set it to "on":

将其设置为“开”:

setsebool httpd_can_network_connect=1

Turn selinux back on: setenforce 1

重新开启 selinux:setenforce 1

Check to be sure php ftp_connectstill works when running under httpd.

检查以确保 phpftp_connect在 httpd 下运行时仍然有效。

Set the policy (-P) to "on" so it persists over a reboot:

将策略 (-P) 设置为“on”,使其在重启后仍然存在:

setsebool -P httpd_can_network_connect=1

Solution 3

解决方案3

There could also be issue with company firewall. Make sure it is configured properly and has access rights set properly.

公司防火墙也可能有问题。确保它配置正确并正确设置了访问权限。

Solution 4

解决方案4

Another approach is to use cURL : libcurlas it can be used to connect and communicate to many different types of servers with many different types of protocols

另一种方法是使用cURL : libcurl ,因为它可用于连接和通信到具有许多不同类型协议的许多不同类型的服务器

Solution 5

解决方案5

There is open source project called PHP Secure Communication Library (phpspeclib)which can be also used to establish secure connection to FTP Server.

有一个名为PHP 安全通信库 (phpspeclib) 的开源项目,它也可用于建立与 FTP 服务器的安全连接。

回答by Frank Nocke

  1. Many cheap webhoster will not give you ssh (hence no ftp via ssh aka sftp) but only ssl-secured ftp aka ftps (see here). You might have that problem. As others suggested, use filezillaor another ftp client to test your credits and chosen security method beforehand.

  2. ftp_ssl_connect() at least under windows will be a long journey, since you have to compile your own php binaries, see here.

  3. As this php contributor rightfully points out, no secure connection is secure, as long as you don't know, who you are talking too, aka ?peer certification“ through valid certificates.

  4. phpseclibis probably your best bet. But I haven't figure out, how to ensure, it uses peer verification (guessing, the truth is in openssl.conf ...)

  5. So even at the time of writing, I wonder more than ever, if peer-validated ftps (ftp with ssl/tls authentification) is possible... also see my question here.

  1. 许多廉价的网络主机不会给你 ssh(因此没有通过 ssh 又名 sftp 的 ftp),而只有 ssl 安全的 ftp 又名 ftps(见这里)。你可能有这个问题。正如其他人所建议的那样,请使用filezilla或其他 ftp 客户端预先测试您的信用和选择的安全方法。

  2. ftp_ssl_connect() 至少在 windows 下将是一个漫长的旅程,因为您必须编译自己的 php 二进制文件,请参阅此处

  3. 正如这个 php 贡献者正确指出的那样,没有安全连接是安全的,只要你不知道,你在谈论谁,也就是通过有效证书的“同行认证”。

  4. phpseclib可能是你最好的选择。但是我还没有弄清楚,如何确保,它使用对等验证(猜测,真相在openssl.conf ...)

  5. 因此,即使在撰写本文时,我也比以往任何时候都更想知道,经过同行验证的 ftps(带有 ssl/tls 身份验证的 ftp)是否可行……也可以在这里查看我的问题

回答by Frank Nocke

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.

至于“ftp via ssh”别名“sftp”:没有直接建议,但请注意,许多廉价的“非专用服务器”网络主机不支持它(这很糟糕)。公司防火墙可能会阻止相关端口。

As for 'ftp using ssl/tls' alias 'ftps':Your answer is here. Don't waste time on ftp_ssl_connect():-)

至于'ftp using ssl/tls'别名'ftps':你的答案在这里。不要浪费时间ftp_ssl_connect():-)

(Yes, it's poorly documented on the php site, to say the least)

(是的,至少可以说,它在 php 站点上的记录很差)

回答by Ifnot

I tried the phpseclib libraryand it works in Windows and Linux.

我尝试了phpseclib 库,它适用于 Windows 和 Linux。

If you are using composer, just add in your require section :

如果您使用作曲家,只需在您的需求部分添加:

"phpseclib/phpseclib": "0.3.*@dev"

And then, you can do this : http://phpseclib.sourceforge.net/sftp/examples.html#put

然后,你可以这样做:http: //phpseclib.sourceforge.net/sftp/examples.html#put

回答by Benoit Vidis

I, too, encountered quite a lot of issues when dealing with encrypted connections.

在处理加密连接时,我也遇到了很多问题。

First thing is to check the protocol you are looking to use. Secure FTP is most commonly refearing to FTP over SHH but can also mean SCP, SFTP or FTPS.

首先是检查您要使用的协议。安全 FTP 最常指的是基于 SHH 的 FTP,但也可以指 SCP、SFTP 或 FTPS。

One way to figure out is to check connecting using a client like filezilla.

一种解决方法是使用像 filezilla 这样的客户端检查连接。

If the protocol is handled via a PHP module, the best approach is indeed, to use it. In this case, you need to make sure that, in addition to the protocol-related one, the OPENSSL module is installed for php.

如果协议是通过 PHP 模块处理的,最好的方法确实是使用它。在这种情况下,您需要确保除了与​​协议相关的模块外,还为 php 安装了 OPENSSL 模块。

There are some cases where the module support still won't work. In this case, using the libcurl module is one option. This is the case for instance when you need to use a client certificate.

在某些情况下,模块支持仍然不起作用。在这种情况下,使用 libcurl 模块是一种选择。例如,当您需要使用客户端证书时就是这种情况。

Unfortunately, here again, you may encounter some problems due to the partial support of libcurl in the php module. One scenario I experimented is when the server certificate is judged invalid by the module.

不幸的是,在这里,由于 php 模块中对 libcurl 的部分支持,您可能会遇到一些问题。我试验过的一种情况是服务器证书被模块判断为无效。

The last solution I usually use is to run the curl binary from an exec statement, for the later case using the "-k" switch.

我通常使用的最后一个解决方案是从 exec 语句运行 curl 二进制文件,对于使用“-k”开关的后一种情况。