Using jopi paranoid fi's example, tmpfile() works on PHP 4 and 5 instead of using the php://temp file.
ftp_fput
(PHP 4, PHP 5)
ftp_fput — Carica un file aperto sul server FTP
Descrizione
bool ftp_fput
( resource $ftp_stream
, string $remote_file
, resource $handle
, int $mode
[, int $startpos
] )
ftp_fput() carica i dati del file cui fa riferimento il puntatore handle fino a quando viene raggiunta la fine del file. I risultati sono salvati in remote_file sul server FTP. Il modo di trasferimento mode specificato deve essere FTP_ASCII oppure FTP_BINARY.
Example #1 Esempio di funzione ftp_fput()
<?php
// apre in lettura un qualche file
$file = 'somefile.txt';
$fp = fopen($file, 'r');
// stabilisce la connessione
$conn_id = ftp_connect($ftp_server);
// si collega con nome utente e password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// prova a caricare $file
if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) {
echo "$file caricato con successo\n";
} else {
echo "Problemi con il caricamento di $file\n";
}
// chiude la connessione e l'handle del file
ftp_close($conn_id);
fclose($fp);
?>
Nota: Il parametro startpos e' stato aggiunto in PHP 4.3.0.
Restituisce TRUE in caso di successo, FALSE in caso di fallimento.
Vedere anche ftp_put(), ftp_nb_fput(), e ftp_nb_put().
ftp_fput
robert b
12-Sep-2008 02:43
12-Sep-2008 02:43
jopi paranoid fi
20-May-2008 12:57
20-May-2008 12:57
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);
?>
info at daniel-marschall dot de
19-Apr-2006 12:04
19-Apr-2006 12:04
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.
BobFrank <rsfranc at yahoo dot com dot br>
23-Aug-2003 09:10
23-Aug-2003 09:10
FTP upload server 2 server
<?php
$FTP_HOST ="ftp.br.geocities.com";
$FTP_USER ="bobfrank";
$FTP_PW ="mypasswd";
$FTP_ROOT_DIR="/";
$LOCAL_SERVER_DIR = "images/";
$FTP_DIR = "mydir/";
$handle=opendir($LOCAL_SERVER_DIR);
while (($file = readdir($handle))!==false)
{
if(!is_dir($file)){
$f[]="$file";
}
}
closedir($handle);
sort($f);
$count=0;
$mode = FTP_BINARY; // or FTP_ASCII
$conn_id = ftp_connect($FTP_HOST);
if(ftp_login($conn_id, $FTP_USER, $FTP_PW)){
print "from: ".$LOCAL_SERVER_DIR."<br>";
print "to: ".$FTP_HOST.$FTP_ROOT_DIR.$FTP_DIR."<br>";
ftp_pwd($conn_id);
ftp_mkdir($conn_id,$FTP_DIR);
ftp_chdir($conn_id,$FTP_DIR);
foreach($f as $files) {
$from = fopen($LOCAL_SERVER_DIR.$files,"r");
if(ftp_fput($conn_id, $files, $from, $mode)){
$count +=1;
print $files."<br>";
}
}
ftp_quit($conn_id);
}
print "upload : $count files.";
?>
darian lassan at yahoo de
04-Feb-2003 10:00
04-Feb-2003 10:00
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.
