6
votes

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
3
Its seems that the connection creation was failed, all PHP ftp extension functions takes a parameter of type resource as a first param which returned with ftp_connect function, however the 'expects parameter 1 to be resource' error means the resource is 'false'. - El Amrani Shakir

3 Answers

11
votes

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

4
votes

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

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.

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:

<?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()

2
votes

Try '127.0.0.1' instead of 'localhost'

ftp_connect('127.0.0.1', 21);