FTP 函数
在线手册:中文  英文

ftp_fput

(PHP 4, PHP 5)

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

说明

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

ftp_fput() 函数用来上传一个在已经打开的文件中的数据到 FTP 服务器。

参数

ftp_stream

FTP 连接的链接标识符。

remote_file

远程文件路径。

handle

打开的本地文件句柄,读取到文件末尾。

mode

传输模式只能为 (文本模式) FTP_ASCII 或 (二进制模式) FTP_BINARY 其中的一个。

startpos

远程文件上传的开始位置。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

Example #1 ftp_fput() 例子

<?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$fpFTP_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);

?>

更新日志

版本 说明
4.3.0 添加了 startpos 的支持。

参见


FTP 函数
在线手册:中文  英文

用户评论:

roy at user dot nl (2013-04-29 08:22:09)

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,$streamFTP_BINARY);

?>

php at cpis dot me (2012-05-11 14:32:53)

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.

jevin (2011-11-22 23:24:25)

You might also want to note that ftp_fput will overwrite any existing file.

timgolding_10 at hotmail dot com (2009-04-26 07:48:53)

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
($resourcetrue);
?>

Before executing any put commands. Took me so long to figure this out I actually cheered when I did :D

rok dot meglic at gmail dot com (2008-11-07 04:35:12)

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$locpathFTP_BINARY);
?>

robert b (2008-09-12 14:43:31)

Using jopi paranoid fi's example, tmpfile() works on PHP 4 and 5 instead of using the php://temp file.

jopi paranoid fi (2008-05-20 00:57:52)

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$tempHandleFTP_ASCII);

?>

Charlie Brown (2008-04-16 14:19:24)

Fails if destination file exists. Delete first and it works.

info at daniel-marschall dot de (2006-04-19 12:04:30)

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> (2003-08-23 21:10:43)

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 (2003-02-04 10:00:17)

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.

易百教程