无法使用来自本地主机的 PHP ftp_connect 连接到 FTP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12462898/
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 03:37:23  来源:igfitidea点击:

Can't connect to FTP with PHP ftp_connect from localhost

phpapacheftpcentos

提问by Gilberto Albino

I I've been trying to make some script to download files through FTP from my localhost Apache in CentOS and I can't get it to work!

我一直在尝试编写一些脚本来通过 FTP 从 CentOS 中的本地主机 Apache 下载文件,但我无法让它工作!

The code I am using is the very one used by any basic ftp request:

我正在使用的代码是任何基本 ftp 请求所使用的代码:

<?php
$ip= FTP_IP_HERE;
$port='21';
$timeout='90';
$un='username';
$pw='password';

// Connect to ftp
$conn_id = ftp_connect($ip,$port,$timeout);

// Open a session to an external ftp site
$login_result = ftp_login ($conn_id, $un, $pw);

// Check open
if ((!$conn_id) || (!$login_result)) {
    print "FTP connection failed!";
    exit();
}

// turn on passive mode transfers
if (ftp_pasv($conn_id, true) == FALSE) {
    print "Passive FTP connection failed!";
    exit();
}

I tried the same script on remote server and it worked! I am not sure if it is any apache configuration to be done, or a PHP limitation itself.

我在远程服务器上尝试了相同的脚本,并且成功了!我不确定是否要完成任何 apache 配置,或者是 PHP 限制本身。

UPDATE:

更新

Here is the error log:

这是错误日志:

Warning: ftp_login() expects parameter 1 to be resource, boolean given in /var/www/html/ftp/FTP.php on line 16

Warning: ftp_get() expects parameter 1 to be resource, boolean given in /var/www/html/ftp/FTP.php on line 22
Falha ao enviar o arquivo test.pdf<br />Array
(
    [type] => 2
    [message] => ftp_get() expects parameter 1 to be resource, boolean given
    [file] => /var/www/html/ftp/FTP.php
    [line] => 22
)

Warning: ftp_close() expects parameter 1 to be resource, boolean given in /var/www/html/ftp/FTP.php on line 30

回答by Jeremy

Ok, I had the same issue and I found the solution for my case. Posting it here to help others.

好的,我遇到了同样的问题,我找到了适合我的案例的解决方案。将其发布在这里以帮助他人。

My PHP script would fail but I could easily FTP via command line. I verified my firewall wasn't blocking the script and I was not getting any PHP errors in my log...

我的 PHP 脚本会失败,但我可以通过命令行轻松进行 FTP。我确认我的防火墙没有阻止脚本,我的日志中也没有出现任何 PHP 错误......

After searching around, it appeared my issue was SELinux. I didn't want to turn it off so I checked the status of httpd_can_network_connect.

四处搜索后,看来我的问题是SELinux。我不想关闭它,所以我检查了httpd_can_network_connect.

Check your status by running:

通过运行检查您的状态:

getsebool httpd_can_network_connect 

If you get:

如果你得到:

httpd_can_network_connect --> off

This may be your issue.

这可能是你的问题。

Note:

笔记:

If you already have this on:

如果你已经有了这个on

httpd_can_network_connect --> on

or

或者

SELinux is disabled

Then this is not going to solve your issue... Good luck finding your solution.

那么这不会解决你的问题......祝你找到你的解决方案好运。

The Fix

修复

Enable httpd_can_network_connectby running:

httpd_can_network_connect通过运行启用:

setsebool httpd_can_network_connect=1

Test your script again and see if it works. This worked for me so I made sure to set a policy to keep this enabled.

再次测试您的脚本,看看它是否有效。这对我有用,所以我确保设置一个策略来保持启用它。

setsebool -P httpd_can_network_connect=1

NOTE: -Psets the policy so it persists over a reboot

注意:-P设置策略使其在重新启动后仍然存在

回答by Yuriy

First make sure it's not an issue with your local firewall or something. Try to FTP from any other tool, e.g.

首先确保它不是本地防火墙或其他东西的问题。尝试从任何其他工具 FTP,例如

wget --user=username --password='password' ftp://FTP_IP_HERE/file_to_download

If wget fails to connect as well, it's a problem with your network settings.

如果 wget 也无法连接,则是您的网络设置有问题。

If wget passes the test you can also try enabling verbose error reporting to see what's wrong with your PHP attemp by placing this at the top of your code:

如果 wget 通过测试,您还可以尝试启用详细错误报告,通过将其放在代码顶部来查看您的 PHP 尝试有什么问题:

<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);

Finally, this might be also relevant to your case: Cannot connect with FTP server with PHP , ftp_connect()

最后,这可能也与您的情况有关:Cannot connect with FTP server with PHP , ftp_connect()

回答by Mike Milkman

Try '127.0.0.1' instead of 'localhost'

尝试 '127.0.0.1' 而不是 'localhost'

ftp_connect('127.0.0.1', 21);