PHP Doku:: Überträgt eine geöffnete Datei auf einen FTP-Server - function.ftp-fput.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige DiensteFTPFTP-Funktionenftp_fput

Ein Service von Reinhard Neidl - Webprogrammierung.

FTP-Funktionen

<<ftp_fget

ftp_get_option>>

ftp_fput

(PHP 4, PHP 5)

ftp_fputÜberträgt eine geöffnete Datei auf einen FTP-Server

Beschreibung

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

ftp_fput() lädt die Daten aus einem Dateizeiger in eine entfernte Datei auf dem FTP-Server.

Parameter-Liste

ftp_stream

Der Verbindungshandler der FTP-Verbindung.

remote_file

Der Pfad zur Datei auf dem Server.

handle

Ein geöffneter Dateizeiger der lokalen Datei. Das Lesen wird am Dateiende beendet.

mode

Der Transfer-Modus. Muss entweder FTP_ASCII oder FTP_BINARY sein.

startpos

Rückgabewerte

Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.

Beispiele

Beispiel #1 ftp_fput()-Beispiel

<?php

// Öffne eine Datei zum Lesen
$file 'somefile.txt';
$fp fopen($file'r');

// Verbindung aufbauen
$conn_id ftp_connect($ftp_server);

// Login mit Benutzername und Passwort
$login_result ftp_login($conn_id$ftp_user_name$ftp_user_pass);

// Versuche $file hochzuladen
if (ftp_fput($conn_id$file$fpFTP_ASCII)) {
    echo 
"$file wurde erfolgreich hochgeladen\n";
} else {
    echo 
"$file konnte nicht hochgeladen werden\n";
}

// Verbindung und Verbindungshandler schließen
ftp_close($conn_id);
fclose($fp);

?>

Changelog

Version Beschreibung
4.3.0 startpos wurde hinzugefügt.

Siehe auch

  • ftp_put() - Lädt eine Datei auf einen FTP-Server
  • ftp_nb_fput() - Speichert eine geöffnete Datei auf den FTP-Server (nicht blockierend)
  • ftp_nb_put() - Speichert eine Datei auf dem FTP-Server (nicht-blockierend)


8 BenutzerBeiträge:
- Beiträge aktualisieren...
timgolding_10 at hotmail dot com
26.04.2009 16:48
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
rok dot meglic at gmail dot com
7.11.2008 13:35
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);
?>
robert b
12.09.2008 23:43
Using jopi paranoid fi's example, tmpfile() works on PHP 4 and 5 instead of using the php://temp file.
jopi paranoid fi
20.05.2008 9:57
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);

?>
Charlie Brown
16.04.2008 23:19
Fails if destination file exists. Delete first and it works.
info at daniel-marschall dot de
19.04.2006 21: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
24.08.2003 6: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
4.02.2003 19: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.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)