Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : FTP Functions : ftp_fput

ftp_fput

Uploads from an open file to the FTP server (PHP 4, PHP 5)
bool ftp_fput ( resource ftp_stream, string remote_file, resource handle, int mode [, int startpos] )

Example 725. ftp_fput() example

<?php

// open some file for reading
$file = 'somefile.txt';
$fp = fopen($file, 'r');

// 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 upload $file
if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) {
   echo
"Successfully uploaded $file\n";
} else {
   echo
"There was a problem while uploading $file\n";
}

// close the connection and the file handler
ftp_close($conn_id);
fclose($fp);

?>

Code Examples / Notes » ftp_fput

info

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.


darian lassan

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.

bobfrank
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."
";
print "to: ".$FTP_HOST.$FTP_ROOT_DIR.$FTP_DIR."
";
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."
";
}
}
ftp_quit($conn_id);
}
print "upload : $count files.";
?>


Change Language


Follow Navioo On Twitter
ftp_alloc
ftp_cdup
ftp_chdir
ftp_chmod
ftp_close
ftp_connect
ftp_delete
ftp_exec
ftp_fget
ftp_fput
ftp_get_option
ftp_get
ftp_login
ftp_mdtm
ftp_mkdir
ftp_nb_continue
ftp_nb_fget
ftp_nb_fput
ftp_nb_get
ftp_nb_put
ftp_nlist
ftp_pasv
ftp_put
ftp_pwd
ftp_quit
ftp_raw
ftp_rawlist
ftp_rename
ftp_rmdir
ftp_set_option
ftp_site
ftp_size
ftp_ssl_connect
ftp_systype
eXTReMe Tracker