downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

ftp_mdtm> <ftp_get
[edit] Last updated: Fri, 26 Apr 2013

view this page in

ftp_login

(PHP 4, PHP 5)

ftp_loginLogs in to an FTP connection

Description

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 #1 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);  
?>



ftp_mdtm> <ftp_get
[edit] Last updated: Fri, 26 Apr 2013
 
add a note add a note User Contributed Notes ftp_login - [3 notes]
up
0
mattsch at gmail dot com
2 years ago
Note that to make an anonymous ftp connection, you need to specify "anonymous" as the username and "" (empty string) as the password.

Example:
<?php
ftp_login
('ftp.example.com', 'anonymous', '');
?>
up
-1
Guibod
8 years ago
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";
?>
up
-3
Patrick Otto
10 months ago
<?php

# This function fix problems with ftp login validation

function connect_send_ftp($ftp_server, $ftp_user_name, $ftp_user_pass, $remote_name, $local_name) {
    
// valid host connection
   
if (!ftp_connect($ftp_server))
         return
false;
    
$conn_id = ftp_connect($ftp_server);
        
   
// valid ftp login params, fast check
    
if(empty($ftp_user_name) or empty($ftp_user_pass))
         return
false;
    
   
// using curl to valid login, fast check   
   
$curl = curl_init();
    
curl_setopt($curl, CURLOPT_URL,'ftp://'.preg_replace('/^ftp[:]\/\//i','',$ftp_server));
    
curl_setopt($curl, CURLOPT_FTPLISTONLY, 1);
    
curl_setopt($curl, CURLOPT_USERPWD, $ftp_user_name.":".$ftp_user_pass);
    
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    
$valid_login = curl_exec ($curl);   
   
   
// valid ftp login, primary
    
if(!$valid_login)
         return
false;
 
   
// valid ftp login, secondary
    
if(!ftp_login($conn_id, $ftp_user_name, $ftp_user_pass))
         return
false;
 
   
// passive result   
   
$passive_result = ftp_pasv($conn_id, true);
    
   
// save file send erros
    
$error = ftp_put($conn_id, $remote_name, $local_name, FTP_ASCII);
 
   
// close connection
    
ftp_close($conn_id);
   
   
// return success or send erros
    
return $error;
 }

// set params
$ftp_server = "ftp.example.com"
$ftp_user_name = "user";
$ftp_user_pass = "password";
$remote_name = "my_remote_file_dir";
$local_name = "my_local_file_dir";

// call the function
$conn = connect_send_ftp($ftp_server, $ftp_user_name, $ftp_user_pass, $remote_name, $local_name);

// valid response
if(!$conn)
    die(
"FTP login failed..")

// if not erros, success
echo "FTP login and file sended sucessfull!"

?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites