不能与从本地主机PHP FTP_CONNECT连接到FTP [英] Can't connect to FTP with PHP ftp_connect from localhost
问题描述
我我一直试图做一些脚本通过FTP从我的本地主机的Apache CentOS中下载文件,我无法得到它的工作!
在code我现在用的是很一按任何基本的FTP请求中使用:
< PHP
$ IP = FTP_IP_HERE;
$端口= '21';
$超时= '90';
$ UN ='用户名';
$ PW ='密码';//连接到FTP
$ conn_id = FTP_CONNECT($ IP,端口$,$超时);//打开一个会话到外部FTP站点
$ login_result = ftp_login($ conn_id,联合国$,$ PW);//检查开
如果((!$ conn_id)||(!$ login_result)){
打印FTP连接失败!;
出口();
}//打开被动模式传输
如果(ftp_pasv($ conn_id,真实)== FALSE){
打印被动FTP连接失败!;
出口();
}
我试过远程服务器上相同的脚本和它的工作!
我不知道这是否是任何Apache配置来完成,或者PHP本身的限制。
更新
下面是错误日志:
警告:ftp_login()预计参数1是资源,布尔上线16 /var/www/html/ftp/FTP.php给出警告:ftp_get()预计参数1是资源,布尔线22 /var/www/html/ftp/FTP.php给出
Falha AO投递Øarquivo的test.pdf< BR />数组
(
[类型] = GT; 2
[消息] => ftp_get()预计参数1是资源,鉴于布尔
[文件] => /var/www/html/ftp/FTP.php
[行] => 22
)警告:ftp_close()预计参数1是资源,布尔线30 /var/www/html/ftp/FTP.php给出
好吧,我有同样的问题,我发现我的情况的解决方案。在这里张贴帮助别人。
我的PHP脚本会失败,但我通过命令行可以很容易地FTP。我验证了我的防火墙没有阻止脚本,我并没有在我的日志得到任何PHP错误...
周围搜索后,就出现了我的问题是的SELinux
。我不想把它关掉,所以我检查的状态 httpd_can_network_connect
。
运行检查状态:
getsebool httpd_can_network_connect
如果你得到:
httpd_can_network_connect - >离
这可能是你的问题。
注意:
如果你已经有这个在
:
httpd_can_network_connect - >上
或
的SELinux被禁用
那么这是不会解决您的问题...祝你好运找到你的解决方案。
的修复
启用 httpd_can_network_connect
运行:
setsebool httpd_can_network_connect = 1
再次测试脚本,看看它是否工作。这为我工作,所以我确信设置策略来保持这个功能。
setsebool -P httpd_can_network_connect = 1
注: -P
设置策略,以便它坚持在重新启动
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!
The code I am using is the very one used by any basic ftp request:
<?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.
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
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...
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
.
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
:
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_connect
by running:
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: -P
sets the policy so it persists over a reboot
这篇关于不能与从本地主机PHP FTP_CONNECT连接到FTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!