PHP 5.6.0 released

ftp_fput

(PHP 4, PHP 5)

ftp_fputCarga un archivo abierto en el servidor FTP

Descripción

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

ftp_fput() carga los datos desde un puntero de archivo a un archivo remoto en el servidor FTP.

Parámetros

ftp_stream

El identificador de enlace de la conexión FTP.

remote_file

La ruta del archivo remoto.

handle

Un apuntador de archivo abierto en el archivo local. La lectura se detiene al final del archivo.

mode

El modo de transferencia. Debe ser FTP_ASCII o FTP_BINARY.

startpos

La posición en el archivo remoto para empezar a subirlo.

Valores devueltos

Devuelve TRUE en caso de éxito o FALSE en caso de error.

Ejemplos

Ejemplo #1 Ejemplo de ftp_fput()

<?php

// abrir un archivo para su lectura
$file 'somefile.txt';
$fp fopen($file'r');

// establecer una conexión básica
$conn_id ftp_connect($ftp_server);

// iniciar sesion con nombre de usuario y contraseña
$login_result ftp_login($conn_id$ftp_user_name$ftp_user_pass);

// intentar cargar $file
if (ftp_fput($conn_id$file$fpFTP_ASCII)) {
    echo 
"Cargado correctamente $file\n";
} else {
    echo 
"Ha habido un problema al cargar $file\n";
}

// cerrar la conexion ftp y el gestor de archivo
ftp_close($conn_id);
fclose($fp);

?>

Ver también

  • ftp_put() - Carga un archivo al servidor FTP
  • ftp_nb_fput() - Almacena un archivo desde un archivo abierto en el servidor FTP (modo no-bloqueo)
  • ftp_nb_put() - Almacena un archivo en el servidor FTP (modo no-bloqueo)

add a note add a note

User Contributed Notes 10 notes

up
5
jopi paranoid fi
6 years ago
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);

?>
up
3
roy at user dot nl
1 year 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 suggested here.

<?php

$string
= "Your content goes here";
$stream = fopen('data://text/plain,' . $string,'r');

ftp_fput($this->connection,$pathTo,$stream, FTP_BINARY);

?>
up
1
rok dot meglic at gmail dot com
5 years ago
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, $locpath, FTP_BINARY);
?>
up
0
php at cpis dot me
2 years ago
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.
up
0
jevin
2 years ago
You might also want to note that ftp_fput will overwrite any existing file.
up
0
timgolding_10 at hotmail dot com
5 years ago
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
($resource, true);
?>

Before executing any put commands. Took me so long to figure this out I actually cheered when I did :D
up
0
robert b
5 years ago
Using jopi paranoid fi's example, tmpfile() works on PHP 4 and 5 instead of using the php://temp file.
up
0
Charlie Brown
6 years ago
Fails if destination file exists. Delete first and it works.
up
0
info at daniel-marschall dot de
8 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_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.
up
0
darian lassan at yahoo de
11 years ago
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.
To Top