Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : FTP Functions : ftp_login

ftp_login

Logs in to an FTP connection (PHP 4, PHP 5)
bool ftp_login ( resource ftp_stream, string username, string password )

Logs in to the given FTP stream.

Parameters

ftp_stream

The link identifier of the FTP connection.

username

The username (USER).

password

The password (PASS).

Return Values

Returns TRUE on success or FALSE on failure. If login fails, PHP will also throw a warning.

Examples

Example 728. ftp_login() example

<?php
                   
$ftp_server
= "ftp.example.com";
$ftp_user = "foo";
$ftp_pass = "bar";

// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");

// try to login
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
   echo
"Connected as $ftp_user@$ftp_server\n";
} else {
   echo
"Couldn't connect as $ftp_user\n";
}

// close the connection
ftp_close($conn_id);  
?>


Code Examples / Notes » ftp_login

guibod

ftp_login does not support ftp trough proxy authentication. So think about using the new PHP5 function ftp_raw() that's allow you to send directly FTP commands. ftp_raw() allow you to send commands prior to be connected (unlike ftp_exec()).
I've writen this piece of code that's allow you to connect through a "remoteid@remotehost proxyid" proxy.
<?php
function ftp_parse_response($response, &$errstr) {
   if(!is_array($response)) {
       $errstr    = 'Parameter \$response must be an array';
       return false;
   }
       
   foreach($response as $r) {
       $code    = substr(trim($r),0,3);
       if(!is_numeric($code)) {
           $errstr    = "$code is not a valid FTP code",$code);
       }
           
       if($code > 400) {
           $errstr    = $r;
           return false;
       }
   }
       
   return true;
}
$user = "user";
$pass = "password";
$host = "ftp.example.com";
$proxyuser = "proxyuser";
$proxypass = "proxypass";
$proxyhost = "ftp.proxy.com";
$conn_id = ftp_connect($proxyhost);
if(!$conn_id) {
   die("cannot connect to proxy");
}
$commands   = array(
   "USER ".$user."@".$host." ".$proxyuser,
   "PASS ".$pass,
   "PASS ".$proxypass
);
foreach($commands as $c) {
   $ret    = ftp_raw($conn_id,$c);
   //you can write your own ftp_parse_response func that
   //use an array of string as input
   if(!ftp_parse_response($ret,$errstr)) {
       ftp_close($conn_id);
       die("cannot login to $host");
   }
}
echo "ok, now connected";
?>


Change Language


Follow Navioo On Twitter
ftp_alloc
ftp_cdup
ftp_chdir
ftp_chmod
ftp_close
ftp_connect
ftp_delete
ftp_exec
ftp_fget
ftp_fput
ftp_get_option
ftp_get
ftp_login
ftp_mdtm
ftp_mkdir
ftp_nb_continue
ftp_nb_fget
ftp_nb_fput
ftp_nb_get
ftp_nb_put
ftp_nlist
ftp_pasv
ftp_put
ftp_pwd
ftp_quit
ftp_raw
ftp_rawlist
ftp_rename
ftp_rmdir
ftp_set_option
ftp_site
ftp_size
ftp_ssl_connect
ftp_systype
eXTReMe Tracker