phpday 2027 - Call for Papers

ftp_connect

(PHP 4, PHP 5, PHP 7, PHP 8)

ftp_connect — Apre una connessione FTP

Descrizione

function ftp_connect(string $hostname, int $port = 21, int $timeout = 90): FTP\Connection|false

ftp_connect() apre una connessione FTP verso l'host hostname specificato.

Elenco dei parametri

hostname

L'indirizzo del server FTP. Questo parametro non deve terminare con delle barre e non deve essere preceduto da ftp://.

port

Questo parametro specifica una porta alternativa a cui connettersi. Se è omesso o impostato a zero, verrà usata la porta FTP predefinita, la 21.

timeout

Questo parametro specifica il timeout in secondi per tutte le successive operazioni di rete. Se omesso, il valore predefinito è di 90 secondi. Il timeout può essere modificato e interrogato in qualsiasi momento con ftp_set_option() e ftp_get_option().

Valori restituiti

Restituisce un'istanza di FTP\Connection in caso di successo o false in caso di fallimento.

Errori/Eccezioni

Lancia un ValueError se timeout è minore o uguale a 0.

Log delle modifiche

Versione Descrizione
8.1.0 Ora restituisce un'istanza di FTP\Connection; in precedenza veniva restituita una resource.
8.0.0 Ora viene lanciato un ValueError se timeout è minore o uguale a 0; in precedenza veniva emesso un E_WARNING e veniva restituito false.

Esempi

Example #1 Esempio di ftp_connect()

<?php

$ftp_server = "ftp.example.com";

// stabilisce una connessione o termina
$ftp = ftp_connect($ftp_server) or die("Impossibile connettersi a $ftp_server");

?>

Vedere anche:

+add a note

User Contributed Notes 1 note

up
16
sean at boyercentral dot net
17 years ago
Ever needed to create an FTP connection resource defaulted to a particular dir from a URI? Here's a simple function that will take a URI like ftp://username:password@subdomain.example.com/path1/path2/, and return an FTP connection resource.

<?php
function getFtpConnection($uri)
{
    // Split FTP URI into:
    // $match[0] = ftp://username:password@sld.domain.tld/path1/path2/
    // $match[1] = ftp://
    // $match[2] = username
    // $match[3] = password
    // $match[4] = sld.domain.tld
    // $match[5] = /path1/path2/
    preg_match("/ftp:\/\/(.*?):(.*?)@(.*?)(\/.*)/i", $uri, $match);

    // Set up a connection
    $conn = ftp_connect($match[1] . $match[4] . $match[5]);

    // Login
    if (ftp_login($conn, $match[2], $match[3]))
    {
        // Change the dir
        ftp_chdir($conn, $match[5]);

        // Return the resource
        return $conn;
    }

    // Or retun null
    return null;
}
?>
To Top