how to connect to an ftp server

Asked

Viewed 44 times

0

I have this code I wanted to connect to ftp. Through the browser I can, but the code tells me you can’t log in.

Follow the code, I hid the data:

$ftp_server = "" ;
$ftp_user_name = "" ;
$ftp_user_pass = "" ;
$depth = 10;

$conn_id = ftp_connect($ftp_server) ;
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (!$login_result) { die("Couldn't log in to FTP account."); }

$dir = array(".");
$a = count($dir);
$i = 0;
while (($a != $b) && ($i < $depth)) {
  $i++;
  $a = count($dir) ;
  foreach ($dir as $d) {
    $ftp_dir = $d."/" ;
    $newdir = ftp_nlist($conn_id, $ftp_dir);
    foreach ($newdir as $key => $x) {
      if ((strpos($x,".")) || (strpos($x,".") === 0)) { unset($newdir[$key]); }
      elseif (!in_array($x,$dir)) { $dir[] = $x ; }
    }
  }
  $b = count($dir) ;
}

print_r($dir) ;

ftp_close($conn_id);

?>

1 answer

0

Long live,

I usually use this class you find on github: https://github.com/Nicolab/php-ftp-client

$ftp = new \FtpClient\FtpClient();
$ftp->connect($host, true, 22);
$ftp->login($login, $password);
$ftp->putAll($source_directory, $target_directory);

It is very easy to use, and has magnificent error control ;) You can also login by ssl.

  • well the point is that I have several directories...I wanted to list and download...never used this class

Browser other questions tagged

You are not signed in. Login or sign up in order to post.