freshman2017
Goto Top

PHP SFTP Upload schlägt fehl

Guten Tag,

hier PHP - FTP Upload via cURL konnte mir schon gut weitergeholfen werden.
Ich habe das ganze jetzt auf SFTP umgestellt und wollte es testen:

<?php
$ch = curl_init();
$localfile = '/path/to/file.zip';  
$remotefile = 'filename.zip';  
$fp = fopen($localfile, 'r');  
curl_setopt($ch, CURLOPT_URL, 'sftp://ftp_login:password@ftp.domain.com/'.$remotefile);  
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
if ($error_no == 0) {
    $error = 'File uploaded succesfully.';  
} else {
    $error = 'File upload error.';  
}
?>

Lokal funktioniert es wunderbar, sobald ich es auf den Webserver bei 1&1 hochschiebe, erhalte ich keine Fehlermeldung, die Dateien werden auch nicht hochgeladen.

Hat da jemand einen Tipp?

Gibt es vielleicht eine elegantere Lösung?

Content-Key: 3008737072

Url: https://administrator.de/contentid/3008737072

Printed on: April 27, 2024 at 01:04 o'clock

Member: StefanKittel
StefanKittel Jun 07, 2022 updated at 12:03:12 (UTC)
Goto Top
So for diagnosing SSH / SFTP problems I think phpseclib, a pure PHP SFTP implementation, is the best approach. Here's how:

<?php
include('Net/SFTP.php');

define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);

$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
exit('Login Failed');
}

$sftp->put('text.txt', 'text.txt', NET_SFTP_LOCAL_FILE);

echo $sftp->getSFTPLog();
?>

In particular, what's useful about phpseclib is it's ability to create log files so you can see what's going on.

I think it's easier to use, too, lol, but that's up to you.

Quelle: https://stackoverflow.com/questions/18180564/how-to-upload-file-with-cur ...
Member: em-pie
Solution em-pie Jun 07, 2022 at 12:03:16 (UTC)
Goto Top
Moin,

habe null plan von SFTP mit PHP, aber analytisch würde ich wie folgt vorgehen:
  1. Stimmen die Zugangsdaten?
  2. Darf der Webserver via Port 22 (oder ist es ein anderer?) nach draußen?

  • Mache den Gegentest generell mal mit WinSCP oder Filezilla und verwende die selben Zugangsdaten. Dann wäre Punkt 1 ausgeschlossen, wenn es klappt. Ansonsten solltest du eine passende Fehlermeldung bekommen.
  • Für den zweiten Punkt solltest du eure FW einmal checken face-wink

Ferner kannst du dein Vorhaben auch mal mit hier vergleichen:
$dataFile      = '/path/to/file';  
$sftpServer    = 'sftp.foo.com';  
$sftpUsername  = 'user';  
$sftpPassword  = 'pass';  
$sftpPort      = 22;
$sftpRemoteDir = '/files';  
 
$ch = curl_init('sftp://' . $sftpServer . ':' . $sftpPort . $sftpRemoteDir . '/' . basename($dataFile));  
 
$fh = fopen($dataFile, 'r');  
 
if ($fh) {
    curl_setopt($ch, CURLOPT_USERPWD, $sftpUsername . ':' . $sftpPassword);  
    curl_setopt($ch, CURLOPT_UPLOAD, true);
    curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
    curl_setopt($ch, CURLOPT_INFILE, $fh);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($dataFile));
    curl_setopt($ch, CURLOPT_VERBOSE, true);
 
    $verbose = fopen('php://temp', 'w+');  
    curl_setopt($ch, CURLOPT_STDERR, $verbose);
 
    $response = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
 
    if ($response) {
        echo "Success";  
    } else {
        echo "Failure";  
        rewind($verbose);
        $verboseLog = stream_get_contents($verbose);
        echo "Verbose information:\n" . $verboseLog . "\n";  
    }
}