PHP Doku:: Anmelden einer FTP-Verbindung (Login) - function.ftp-login.html

Verlauf / Chronik / History: (2) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige DiensteFTPFTP-Funktionenftp_login

Ein Service von Reinhard Neidl - Webprogrammierung.

FTP-Funktionen

<<ftp_get

ftp_mdtm>>

ftp_login

(PHP 4, PHP 5)

ftp_loginAnmelden einer FTP-Verbindung (Login)

Beschreibung

bool ftp_login ( resource $ftp_stream , string $username , string $password )

Anmelden beim angegebenen FTP-Stream.

Parameter-Liste

ftp_stream

Der Verbindungshandler der FTP-Verbindung.

username

Der Benutzername (USER).

password

Das Passwort (PASS).

Rückgabewerte

Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben. Wenn die Anmeldung fehlschlägt, wird PHP zusätzlich eine Warnung werfen.

Beispiele

Beispiel #1 ftp_login()-Beispiel

<?php

$ftp_server 
"ftp.example.com";
$ftp_user "foo";
$ftp_pass "bar";

// Verbindung aufbauen
$conn_id ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");

// Anmeldung versuchen
if (@ftp_login($conn_id$ftp_user$ftp_pass)) {
    echo 
"Angemeldet als $ftp_user@$ftp_server\n";
} else {
    echo 
"Anmeldung als $ftp_user nicht möglich\n";
}

// Verbindung schließen
ftp_close($conn_id);
?>


2 BenutzerBeiträge:
- Beiträge aktualisieren...
mattsch at gmail dot com
1.12.2010 5:12
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', '');
?>
Guibod
26.10.2004 10:37
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";
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)