Error connecting ftp via php

I am trying to connect to my server using php script to download some files ...

But it does not connect ...

I do not know what a mistake is ...

I'm sure ftp is on, I checked it through php_info ()

What could be a mistake ...

<?php
error_reporting(E_ALL); 
$ftp_server = "server.com";  //address of ftp server (leave out ftp://)
$ftp_user_name = "Username"; // Username
$ftp_user_pass = "Password";   // Password

$conn_id = ftp_connect($ftp_server);        // set up basic connection

$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);

if ($login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass)) {
    echo "Connected as ,$ftp_user_name,$ftp_user_pass \n";
} else {
    echo "Couldn't connect \n";
}
.....
.....
....
....
ftp_close($conn_id); // close the FTP stream
?>
+3
source share
3 answers

perhaps you need to enable passive mode by doing:

ftp_pasv($conn_id, true);

immediately after ftp_login

PS: why are you doing double entry? to write

$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);

if ($login_result) {

instead

$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);

if ($login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass)) {
+4
source

This looks wrong:

$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);

if ($login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass)) {

You just need to:

$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);

if ($login_result) {

Otherwise, he will try to log in twice, this may be a problem.

or die ftp_conect, , .

$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 
0
  • Verify that the server you are trying to connect to is actually accepting connections from where this script is running using a regular FTP client. Your code may be correct, but the FTP server is not accepting connections from your server or is blocking the firewall.
  • Most PHP functions will write error information internally, which you can get with error_get_last()and / or $ php_errormsg. Some diagnostic information about why the login call does not work can be stored there.
0
source

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


All Articles