ftp_fput

(PHP 3 >= 3.0.13, PHP 4, PHP 5)

ftp_fput -- 上传一个已经打开的文件到 FTP 服务器

说明

bool ftp_fput ( resource ftp_stream, string remote_file, resource handle, int mode [, int startpos] )

ftp_fput() 函数用来上传一个在已经打开的文件中的数据到 FTP 服务器,参数 handle 为所打开文件的句柄。参数 remote_file 为上传到服务器上的文件名。传输模式参数 mode 只能为 (文本模式) FTP_ASCII 或 (二进制模式) FTP_BINARY 其中的一个。

例子 1. ftp_fput() example

<?php

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

// connect to the server
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to upload the 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);

?>

注: 参数 startpos 只适用于 4.3.0 以上版本。

如果成功则返回 TRUE,失败则返回 FALSE

参见 ftp_put()ftp_nb_fput()ftp_nb_put()


add a note add a note User Contributed Notes
info at daniel-marschall dot de
20-Apr-2006 03: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>
24-Aug-2003 12: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
05-Feb-2003 02: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.