ftp_fput

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

ftp_fputTéléverse un fichier sur un serveur FTP

Description

ftp_fput(
    FTP\Connection $ftp,
    string $remote_filename,
    resource $stream,
    int $mode = FTP_BINARY,
    int $offset = 0
): bool

ftp_fput() charge les données issues du fichier identifié par stream jusqu'à la fin du fichier.

Liste de paramètres

ftp

Une instance de FTP\Connection.

remote_filename

Le chemin vers le fichier distant.

stream

Un pointeur de fichier ouvert sur le fichier local. La lecture s'arrête à la fin du fichier.

mode

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

offset

La position dans le fichier distant à partir de laquelle le téléchargement commencera.

Valeurs de retour

Cette fonction retourne true en cas de succès ou false si une erreur survient.

Historique

Version Description
8.1.0 La paramètre ftp attend désormais une instance de FTP\Connection ; auparavant, une resource était attendu.
7.3.0 Le paramètre mode est maintenant optionnel. Précédemment il était obligatoire.

Exemples

Exemple #1 Exemple avec ftp_fput()

<?php

// Ouverture de quelques fichiers pour lecture
$file = 'somefile.txt';
$fp = fopen($file, 'r');

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

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

// Tente de charger le fichier $file
if (ftp_fput($ftp, $file, $fp, FTP_ASCII)) {
echo
"Chargement avec succès du fichier $file\n";
} else {
echo
"Il y a eu un problème lors du chargement du fichier $file\n";
}

// Fermeture de la connexion et du pointeur de fichier
ftp_close($ftp);
fclose($fp);

?>

Voir aussi

  • ftp_put() - Charge un fichier sur un serveur FTP
  • ftp_nb_fput() - Écrit un fichier sur un serveur FTP, et le lit depuis un fichier (non bloquant)
  • ftp_nb_put() - Envoie un fichier sur un serveur FTP (non-bloquant)

add a note

User Contributed Notes 10 notes

up
28
roy at user dot nl
11 years ago
For directly inserting content into a file on an FTP host, you could also create a string stream wich streams directly to the ftp_fput function.

This should create less overhead than first writing to any temp directories locally before streaming, as suggested here.

<?php

$string
= "Your content goes here";
$stream = fopen('data://text/plain,' . $string,'r');

ftp_fput($this->connection,$pathTo,$stream, FTP_BINARY);

?>
up
7
timgolding_10 at hotmail dot com
15 years ago
If when using fput you get the one of the following errors:

Warning: ftp_fput() [function.ftp-fput]: Opening ASCII mode data connection

Warning: ftp_fput() [function.ftp-fput]: Opening BINARY mode data connection

and it creates the file in the correct location but is a 0kb file and all FTP commands thereafter fail. It is likely that the client is behind a firewall. To rectify this use:

<?php
ftp_pasv
($resource, true);
?>

Before executing any put commands. Took me so long to figure this out I actually cheered when I did :D
up
3
jopi paranoid fi
16 years ago
When you have your file contents as a string, create temporary stream and use that as a file handle.

<?php

$contents
= "This is a test file\nTesting 1,2,3..";

$tempHandle = fopen('php://temp', 'r+');
fwrite($tempHandle, $contents);
rewind($tempHandle);

ftp_fput($this->ftp, $filename, $tempHandle, FTP_ASCII);

?>
up
1
rok dot meglic at gmail dot com
16 years ago
Make sure you chdir to remote directory before using ftp_put or else ftp_put will just return error that it cannot create file. After you do the chdir you should NOT pass the whole path of file to ftp_put but just basename (filename). See example for more info.

Example:
<?php
$locpath
= 'local_path/resources/js/test.js';
$rempath = 'resources/js/';
$remFile = 'test.js';

ftp_chdir($this->conn_id, $rempath);
ftp_put($this->conn_id, $remFile, $locpath, FTP_BINARY);
?>
up
0
php at cpis dot me
12 years ago
This might be obvious to most of you, but make sure your stream isn't write-only. It has to be able to read from your stream in order to upload its contents.

Took me a while trying to figure out why my uploaded file was 0B, and that was why.
up
0
jevin
13 years ago
You might also want to note that ftp_fput will overwrite any existing file.
up
0
robert b
16 years ago
Using jopi paranoid fi's example, tmpfile() works on PHP 4 and 5 instead of using the php://temp file.
up
0
Charlie Brown
16 years ago
Fails if destination file exists. Delete first and it works.
up
0
info at daniel-marschall dot de
18 years ago
This is a function i wrote to copy a complete directory to a FTP-Server-folder.

function ftp_uploaddirectory($conn_id, $local_dir, $remote_dir)
{
@ftp_mkdir($conn_id, $remote_dir);
$handle = opendir($local_dir);
while (($file = readdir($handle)) !== false)
{
if (($file != '.') && ($file != '..'))
{
if (is_dir($local_dir.$file))
{
ftp_uploaddirectory($conn_id, $local_dir.$file.'/', $remote_dir.$file.'/');
}
else
$f[] = $file;
}
}
closedir($handle);
if (count($f))
{
sort($f);
@ftp_chdir($conn_id, $remote_dir);
foreach ($f as $files)
{
$from = @fopen("$local_dir$files", 'r');
@ftp_fput($conn_id, $files, $from, FTP_BINARY);
}
}
}

Example:

$conn_id = @ftp_connect($server);
@ftp_login ($conn_id, $username, $passwort);
ftp_uploaddirectory($conn_id, 'mydirectory/', 'theftpdirectory/');
@ftp_quit($conn_id);

I hope you'll find it useful.
up
-3
darian lassan at yahoo de
21 years ago
If you want to pass a string containing the filename as source and not a resource handle use ftp_put() instead. Trivial but not mentioned here.
To Top