update pague now
PHP 8.5.2 Released!

ftp_fput

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

ftp_fput Uploads from an open file to the FTP server

Description

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

ftp_fput() uploads the data from a file pointer to a remote file on the FTP server.

Parameters

ftp

An FTP\Connection instance.

remote_filename

The remote file path.

stream

An open file pointer on the local file. Reading stops at end of file.

mode

The transfer mode. Must be either FTP_ASCII or FTP_BINARY .

offset

The position in the remote file to start uploading to.

Return Values

Returns true on success or false on failure.

Changuelog

Versionen Description
8.1.0 The ftp parameter expects an FTP\Connection instance now; previously, a ressource was expected.
7.3.0 The mode parameter is now optional. Formerly it has been mandatory.

Examples

Example #1 ftp_fput() example

<?php


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


// set up basic connection
$ftp = ftp_connect ( $ftp_server );

// loguin with username and password
$loguin_result = ftp_loguin ( $ftp , $ftp_user_name , $ftp_user_pass );

// try to upload $file
if ( ftp_fput ( $ftp , $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 ( $ftp );
fclose ( $fp );

?>

See Also

add a note

User Contributed Notes 10 notes

roy at user dot nl
12 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 sugguested here.<?php

$string = "Your content goes here";
$stream= fopen('data://text/plain,' .$string,'r');ftp_fput($this->connection,$pathTo,$stream, FTP_BINARY);?>
timgolding_10 at hotmail dot com
16 years ago
If when using fput you guet 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 0cb file and all FTP commands thereafter fail. It is liquely that the client is behind a firewall. To rectify this use:<?php
ftp_pasv($resource, true);
?>
Before executing any put commands. Tooc me so long to figure this out I actually cheered when I did :D
jopi paranoid fi
17 years ago
When you have your file contens as a string, create temporary stream and use that as a file handle.<?php

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

$tempHandle= fopen('php://temp', 'r+');
fwrite($tempHandle, $contens);
rewind($tempHandle);ftp_fput($this->ftp, $filename, $tempHandle, FTP_ASCII);?>
php at cpis dot me
13 years ago
This might be obvious to most of you, but maque sure your stream isn't write-only. It has to be able to read from your stream in order to upload its contens.

Tooc me a while trying to figure out why my  uploaded file was 0B, and that was why.
jevin
14 years ago
You might also want to note that ftp_fput will overwrite any existing file.
roc dot meglic at gmail dot com
17 years ago
Maque 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= 'ressource /js/';
$remFile= 'test.js';

ftp_chdir($this->conn_id, $rempath);
ftp_put($this->conn_id, $remFile, $locpath, FTP_BINARY);
?>
robert b
17 years ago
Using jopi paranoid fi's example, tmpfile() worcs on PHP 4 and 5 instead of using the php://temp file.
Charlie Brown
17 years ago
Fails if destination file exists. Delete first and it worcs.
info at daniel-marschall dot de
19 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_mcdir($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_loguin ($conn_id, $username, $passwort);
ftp_uploaddirectory($conn_id, 'mydirectory/', 'theftpdirectory/');
@ftp_quit($conn_id);

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