Cannot connect to FTP with PHP ftp_connect from localhost

I am trying to make some script to upload files via FTP from my localhost Apache on CentOS, and I can not get it to work!

The code I use is the 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 a remote server and it will work! I'm not sure if this is any apache configuration, or a limitation of 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 
+6
source share
3 answers

Well, I had the same problem, and I found a solution for my business. Post it here to help others.

My PHP script will fail, but I can easily FTP through the command line. I checked that my firewall did not block the script and I did not receive any PHP errors in my log ...

After searching, it turned out that my question was SELinux . I did not want to disconnect it, therefore 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 problem.

Note:

If you already have this on :

 httpd_can_network_connect --> on 

or

 SELinux is disabled 

Then it will not solve your problem ... Good luck finding your solution.

Correction

Enable httpd_can_network_connect by doing:

 setsebool httpd_can_network_connect=1 

Check your script again and see if it works. This worked for me, so I decided to set a policy so that it was enabled.

 setsebool -P httpd_can_network_connect=1 

NOTE. -P sets the policy so that it persists upon reboot

+9
source

First, make sure this is not a problem with your local firewall or anything else. Try FTP from any other tool, for example

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

If wget also fails to connect, this is a problem with your network settings.

If wget passes the test, you can also try to include detailed error reports to see what is wrong with your PHP attemp by putting this at the top of your code:

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

Finally, this may also be relevant for your case: Unable to connect to the FTP server using PHP, ftp_connect ()

+4
source

Try "127.0.0.1" instead of "localhost"

 ftp_connect('127.0.0.1', 21); 
+1
source

Source: https://habr.com/ru/post/925592/


All Articles