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

search for in the

ftp_login> <ftp_get_option
Last updated: Fri, 03 Oct 2008

view this page in

ftp_get

(PHP 4, PHP 5)

ftp_getTélécharge un fichier depuis un serveur FTP

Description

bool ftp_get ( resource $ftp_stream , string $local_file , string $remote_file , int $mode [, int $resumepos ] )

ftp_get() télécharge le fichier remote_file depuis le serveur FTP, et le sauve dans le fichier local local_file .

Liste de paramètres

ftp_stream

L'identifiant du lien de connexion FTP.

local_file

Le chemin vers le fichier local (sera écrasé si le fichier existe déjà).

remote_file

Le chemin vers le fichier distant.

mode

Le mode de transfert. Doit être soit FTP_ASCII, soit FTP_BINARY.

resumepos

La position dans le fichier distant à partir duquel on commence le téléchargement.

Valeurs de retour

Cette fonction retourne TRUE en cas de succès, FALSE en cas d'échec.

Exemples

Exemple #1 Exemple avec ftp_get()

<?php

// Définition de quelques variables
$local_file 'local.zip';
$server_file 'server.zip';

// Mise en place d'une connexion basique
$conn_id ftp_connect($ftp_server);

// Identification avec un nom d'utilisateur et un mot de passe
$login_result ftp_login($conn_id$ftp_user_name$ftp_user_pass);

// Tentative de téléchargement du fichier $server_file et sauvegarde dans le fichier $local_file
if (ftp_get($conn_id$local_file$server_fileFTP_BINARY)) {
    echo 
"Le fichier $local_file a été écris avec succès\n";
} else {
    echo 
"Il y a un problème\n";
}

// Fermeture de la connexion
ftp_close($conn_id);

?>

Historique

Version Description
4.3.0 resumepos a été ajouté.



ftp_login> <ftp_get_option
Last updated: Fri, 03 Oct 2008
 
add a note add a note User Contributed Notes
ftp_get
Aditya P dot Bhatt (adityabhai at gmail dot com)
25-Mar-2008 12:40
<?php
               
// define some variables
       
$folder_path = "YOUR FOLDER PATH";
       
$local_file = "LOCAL FILE PATH";
       
$server_file = "SERVER FILE PATH";
       
       
//-- Connection Settings
       
$ftp_server = "IP ADDRESS"; // Address of FTP server.
       
$ftp_user_name = "USERNAME"; // Username
       
$ftp_user_pass = "PASSWORD"; // Password
        #$destination_file = "FILEPATH";
       
        // set up basic connection
       
$conn_id = ftp_connect($ftp_server);
       
       
// login with username and password
       
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
       
       
// try to download $server_file and save to $local_file
       
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
            echo
"Successfully written to $local_file\n";
        } else {
            echo
"There was a problem\n";
        }
       
       
// close the connection
       
ftp_close($conn_id);
?>
anomie at users dot sf dot net
30-Jan-2007 04:24
Crud. The _nb_ only refers to reading from the ftp server, and the buffer in the socket pair is only about 364 bytes. So it doesn't work for files larger than that size.
anomie at users dot sf dot net
25-Jan-2007 06:50
Why there isn't an "ftp_get_contents" function, I don't know. It takes a little work to emulate one, but it's doable.
<?php
function ftp_get_contents($ftp_stream, $remote_file, $mode, $resume_pos=null){
   
$pipes=stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
    if(
$pipes===false) return false;
    if(!
stream_set_blocking($pipes[1], 0)){
       
fclose($pipes[0]); fclose($pipes[1]);
        return
false;
    }
   
$fail=false;
   
$data='';
    if(
is_null($resume_pos)){
       
$ret=ftp_nb_fget($ftp_stream, $pipes[0], $remote_file, $mode);
    } else {
       
$ret=ftp_nb_fget($ftp_stream, $pipes[0], $remote_file, $mode, $resume_pos);
    }
    while(
$ret==FTP_MOREDATA){
        while(!
$fail && !feof($pipes[1])){
           
$r=fread($pipes[1], 8192);
            if(
$r==='') break;
            if(
$r===false){ $fail=true; break; }
           
$data.=$r;
        }
       
$ret=ftp_nb_continue($ftp_stream);
    }
    while(!
$fail && !feof($pipes[1])){
       
$r=fread($pipes[1], 8192);
        if(
$r==='') break;
        if(
$r===false){ $fail=true; break; }
       
$data.=$r;
    }
   
fclose($pipes[0]); fclose($pipes[1]);
    if(
$fail || $ret!=FTP_FINISHED) return false;
    return
$data;
}
?>

Something similar would work to write a ftp_put_contents function, too.
administrator at gesoft dot org
13-Aug-2006 12:05
Hello everybody,

If someone will try to download files to the same local file (some temporary file), like shown here:

<?php
foreach ($files as $key=>$path) {
...
 
$result = ftp_get($ftpConnId, 'temp.tmp', $path, FTP_BINARY);
...
}
?>

please take in consideration the fact that you will have big problems with downloading (getting) hole files. In other words ‘temp.tmp’ file always will have the same size equal to first downloaded file despite the real size of downloading file. I have not idea what is the reason!

If someone will think that problem is just in getting proper file size (which you will get using filssize() function) he will be mistaken. The download file’s size is not equal to source file’s size materially, that means fflush() function will not solve the problem (I have tried this as well).

Finally the solution was founded: before downloading a file you will need to delete local file if such exist (‘temp.tmp’). So working code will look like:

<?php
foreach ($files as $key=>$path) {
...
  if (
file_exists('temp.tmp')) {
   
unlink('temp.tmp');
  }
 
$result = ftp_get($ftpConnId, 'temp.tmp', $path, FTP_BINARY);
...
}
?>

Good luck in scripting :-)

Vitali Simsive
corey-holzer at nyc dot rr dot com
23-Jan-2004 03:20
The zero size file is not a side effect.  When the ftp_get starts the first thing it does is to create the inode/file which it will stream the data too and that is a zero size file with the nname you specified for the local file.  When the download fails it leaves the file in place.
thivierr at telus dot net
22-Nov-2003 11:25
If you previously downloaded a file before (like a huge web log), and just want to get the remaining portion, do this:

$local_file_size = filesize($local_file_path);
$get_result = ftp_get($conn_id, $local_file_path, $remote_file_path, FTP_BINARY, $local_file_size);

This same code works regardless of wether the local file exists already or not.  You should first test to make sure the local file is not bigger than the remote file.
ramiro at qusarcr dot com
06-Nov-2002 04:36
Keep in mind that ftp_get will overwrite the file on your local machine if it has the same name.

ftp_login> <ftp_get_option
Last updated: Fri, 03 Oct 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites