PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<fribidi_log2visftp_alloc>
view the version of this page
Last updated: Sun, 02 May 2004

XXXIII. Funzioni FTP

Le funzioni in questa estensione implementano l'accesso client ad un file server utilizzando il File Transfer Protocol (FTP) come definito in http://www.faqs.org/rfcs/rfc959.html.

Usando il modulo FTP vengono definite le seguenti costanti: FTP_ASCII e FTP_BINARY.

Per l'utilizzo delle funzioni FTP con la vostra configurazione PHP, dovrete aggiungere l'opzione --enable-ftp durante l'installazione PHP 4, e --with-ftp nell'installazione di PHP 3.

Esempio 1. FTP

<?php
// stabilire una connessione
$conn_id = ftp_connect($ftp_server); 

// login con user name e password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

// controllo della connessione
if ((!$conn_id) || (!$login_result)) { 
        echo "La connessione FTP è fallita!";
        echo "Tentativo di connessione a $ftp_server per l'utente $ftp_user_name"; 
        die; 
    } else {
        echo "Connesso a $ftp_server, utente $ftp_user_name";
    }

// upload del file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

// controllo dello stato di upload
if (!$upload) { 
        echo "Il caricamento FTP non è andato a buon fine!";
    } else {
        echo "Caricato il file $source_file su $ftp_server come $destination_file";
    }

// chiudere il flusso FTP 
ftp_quit($conn_id); 
?>

Sommario
ftp_alloc -- Allocates space for a file to be uploaded.
ftp_cdup -- Passa alla directory superiore
ftp_chdir -- Cambia le directory su un server FTP
ftp_chmod -- Set permissions on a file via FTP
ftp_close -- Chiude una connessione FTP
ftp_connect -- Apre una connessione FTP
ftp_delete -- Cancella un file sul server FTP
ftp_exec -- Richiede l'esecuzione di un programma sul server FTP
ftp_fget -- Scarica un file dal server FTP e lo salva in un file aperto
ftp_fput -- Carica da un file aperto al server FTP
ftp_get_option -- Richiama vari comportamenti runtime dello stream FTP attualmente in uso
ftp_get -- Scarica un file dal server FTP
ftp_login -- Loggarsi in una connessione FTP
ftp_mdtm -- Restituisce la data di ultima modifica di un dato file
ftp_mkdir -- Crea una directory
ftp_nb_continue -- Continues retrieving/sending a file (non-blocking)
ftp_nb_fget -- Retrieves a file from the FTP server and writes it to an open file (non-blocking)
ftp_nb_fput -- Stores a file from an open file to the FTP server (non-blocking)
ftp_nb_get -- Retrieves a file from the FTP server and writes it to a local file (non-blocking)
ftp_nb_put -- Stores a file on the FTP server (non-blocking)
ftp_nlist -- Restituisce l'elenco dei file in una data directory
ftp_pasv -- Abilita o disabilita la modalità passiva
ftp_put -- Carica un file sul server FTP
ftp_pwd -- Restituisce il nome della directory corrente
ftp_quit -- Chiude una connessione FTP
ftp_raw -- Sends an arbitrary command to an FTP server
ftp_rawlist -- Restituisce l'elenco dettagliato dei file in una data directory
ftp_rename -- Rinomina un file sul server FTP
ftp_rmdir -- Elimina una directory
ftp_set_option -- Imposta alcune opzioni runtime generiche dell'FTP
ftp_site -- Invia un comando SITE al server
ftp_size -- Restituisce la dimensione di un dato file
ftp_ssl_connect -- Opens an Secure SSL-FTP connection
ftp_systype -- Restituisce l'identificatore del tipo di sistema del server FTP remoto


add a note add a note User Contributed Notes
Funzioni FTP
postmaster at alishomepage dot com
24-Jan-2004 09:29
I have written an OpenSource ZIP2FTP interface, which actually takes a given ZIP file and decompresses it in the folder on an FTP server you specify...

Therefore it may be quite interesting for you people interested in FTP, its adress is http://zip2ftp.alishomepage.com ; those who directly want the source may visit http://zip2ftp.alishomepage.com/?do=getSource
Vikrant Korde <vakorde at hotmail dot com>
14-Nov-2003 11:35
<?

//If you want to move or replicate the folder hierarchy from your current server to another remote server. Then this will be helpful as this will browse the current server's directory and at the same time it will copy that file in the remote server in the same directory.

//This script will copy all the files from this directory and subdirectory to another remote server via FTP

function rec_copy ($source_path, $destination_path, $con)
{
  
ftp_mkdir($con, $destination_path);
  
ftp_site($con, 'CHMOD 0777 '.$destination_path);
  
ftp_chdir($con,$destination_path);

   if (
is_dir($source_path))
   {
      
chdir($source_path);
      
$handle=opendir('.');
       while ((
$file = readdir($handle))!==false)
       {
           if ((
$file != ".") && ($file != ".."))
           {
               if (
is_dir($file))
               {
                  
// here i am restricting the folder name 'propertyimages' from being copied to remote server. -- VK
                  
if($file != "propertyimages")
                   {
                      
rec_copy ($source_path."/".$file, $file, $con);
                      
chdir($source_path);
                      
ftp_cdup($con);
                   }
               }
               if (
is_file($file))
               {
                  
$fp = fopen($file,"r");
                  
// this will convert spaces to '_' so that it will not throw error.  -- VK
                  
ftp_fput ($con, str_replace(" ", "_", $file), $fp,FTP_BINARY);
                  
ftp_site($con, 'CHMOD 0755 '.str_replace(" ", "_", $file));
               }
           }
       }
      
closedir($handle);
   }
}

// make a FTP connection --VK
$con = ftp_connect("69.18.213.131",21);
$login_result = ftp_login($con,"username","password");   

// this is the root path for the remote server-- VK
$rootpath = "mainwebsite_html";   

// this is the physical path of the source directory. actually u can also use the relative path. -- VK
$sourcepath = realpath("../")."/resdesk";

// this directory name will only change the top most directory and not the inner one -- VK
$destination_dir_name = "resdesk_".$account_id."/";

rec_copy ($sourcepath, $destination_dir_name, $con);
if (
function_exists("ftp_close"))
{
  
ftp_close($con);
}
?>
postmaster at alishomepage dot com
24-Oct-2003 10:06
Here's another FTP interface over PHP (also uses MySQL)

http://myftp.alishomepage.com

PS: this script will ALSO allow you to download its source... So it becomes interesting for YOU PROGRAMMERS as well :D
arjenjb dot wanadoo dot nl
09-Mar-2003 04:29
Check http://nanoftpd.sourceforge.net/ for a FTP server written in PHP.
Supports Passive and Active FTP, and all other standard FTP commands as decribed in RFC959.
php dot net at thedrewerys dot com
08-Feb-2003 09:25
To chmod, you need to use ftp_site instead of ftp_exec.

In the example above,
ftp_exec($ftp_conn, 'chmod 0666 logs/log.txt')
should be
ftp_site($ftp_conn, 'chmod 0666 logs/log.txt')
eric at evilwalrus dot com
11-Jan-2003 04:14
There is no FTP command for the CHMODing of files, so you really have to do it manually using ftp_exec(). I havn't tried this myself but I'm assuming it should work, but here is an example of what I would do.

//After we have connected to the FTP server
//and need to set the perms for a file/dir, use
//this command
if (ftp_exec($ftp_conn, 'chmod 0666 logs/log.txt'))
   echo 'Successfully set permissions for logs/log.txt to 0666';
else
   echo 'Failed to set permissions for logs/log.txt';

Again, I have not tested this, but I assume it would work. Hope that helps.
Marius at lowvoice dot nl
22-Oct-2002 08:51
if you need to download a  file into a  string instead of into a file try this:

$temp = tmpfile();

$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $user, $pass);

$fget_result=ftp_fget($conn_id, $temp, $ftp_file_name, FTP_ASCII);
ftp_quit($conn_id);

rewind ($temp);
$FILE_DATA=fread($temp,$size);
fclose($temp);

you could also do this with fopen() but I found that using this function will keep the connection open even after a fclose()!! So repeating this many times will cause a lot of open ftp-connections.
NOSPAMkent at ioflux dot NOSPAM dot com
19-Sep-2002 11:05
I think what some other posts were trying to say which may need clarification is that in PHP 4.2.3, ftp_connect("myhost.com") was failing most of the time, except it would work like every few minutes.

The fix is that ftp_connect seems to have a bug resolving addresses. If you do:

$hostip = gethostbyname($host);
$conn_id = ftp_connect($hostip);

It seems to solve the problem.
(Other users referred to an ftpbuf() error... not sure what that is, but this should fix it.)
sven at cartell-network dot de
13-Feb-2002 05:27
connection to a ftp server across proxy

$ftp_server = "proxy"; f.e. 123.456.789.10
$ftp_user_name = "username@ftpserver"; f.e. exampleuk@www.example.uk
$ftp_user_pass = "password";

$conn_id = ftp_connect($ftp_server, 2121);
$login_result = ftp_login( $conn_id, $ftp_user_name, $ftp_user_pass );
Paul Southworth
16-Mar-2000 08:27
If you are looking for a sketchy sample implementation of an FTP client written in PHP, here is a free one: 
http://inebria.com/phpftp/

<fribidi_log2visftp_alloc>
 Last updated: Sun, 02 May 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This mirror generously provided by: Italia OnLine S.p.a.
Last updated: Fri May 21 04:11:23 2004 CEST