php 使用 SFTP 上传文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9572314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:09:09  来源:igfitidea点击:

Uploading files with SFTP

phpsftp

提问by Marc

I have successfully uploaded files over ftp, but I now need to do via SFTP. I can successfully connect to the remote server, create a file and write to it, but I am unable to upload an existing file from my local server to the remote server. Is ftp_put not firing with an sftp connection?

我已经通过 ftp 成功上传了文件,但我现在需要通过 SFTP 上传。我可以成功连接到远程服务器,创建一个文件并写入其中,但是我无法将现有文件从本地服务器上传到远程服务器。ftp_put 没有通过 sftp 连接触发吗?

My code used to write a file :

我的代码用来写一个文件:

//Send file via sftp to server

$strServer = "*****";
$strServerPort = "****";
$strServerUsername = "*****";
$strServerPassword = "*****";
$csv_filename = "Test_File.csv";

//connect to server
$resConnection = ssh2_connect($strServer, $strServerPort);

if(ssh2_auth_password($resConnection, $strServerUsername, $strServerPassword)){
    //Initialize SFTP subsystem

    echo "connected";
    $resSFTP = ssh2_sftp($resConnection);    

    $resFile = fopen("ssh2.sftp://{$resSFTP}/".$csv_filename, 'w');
    fwrite($resFile, "Testing");
    fclose($resFile);                   

}else{
    echo "Unable to authenticate on server";
}

Has anyone had any success in grabbing a local file and uploading via a method such as above with sftp? An example would be greatly appreciated.

有没有人在抓取本地文件并通过上面的 sftp 方法上传方面取得了任何成功?一个例子将不胜感激。

Thanks

谢谢

回答by dev-null-dweller

With the method above (involving sftp) you can use stream_copy_to_stream:

使用上述方法(涉及 sftp),您可以使用stream_copy_to_stream

$resFile = fopen("ssh2.sftp://{$resSFTP}/".$csv_filename, 'w');
$srcFile = fopen("/home/myusername/".$csv_filename, 'r');
$writtenBytes = stream_copy_to_stream($srcFile, $resFile);
fclose($resFile);
fclose($srcFile);

You can also try using ssh2_scp_send

您也可以尝试使用ssh2_scp_send

回答by neubert

Personally, I prefer avoiding the PECL SSH2 extension. My preferred approach involves phpseclib, a pure PHP SFTP implementation. Here's an example with phpseclib 2.0 (requires composer):

就个人而言,我更喜欢避免使用 PECL SSH2 扩展。我的首选方法涉及phpseclib,一个纯 PHP SFTP 实现。这是 phpseclib 2.0 的示例(需要 composer):

<?php
require __DIR__ . '/vendor/autoload.php';

use phpseclib\Net\SFTP;

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

$sftp->put('remote.ext', 'local.ext', SFTP::SOURCE_LOCAL_FILE);
?>

Here's that same example with phpseclib 1.0:

这是与 phpseclib 1.0 相同的示例:

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

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

$sftp->put('remote.ext', 'local.ext', NET_SFTP_LOCAL_FILE);
?>

One of the big things I like about phpseclib over the PECL extension is that it's portable. Maybe the PECL extension works on one version of Linux but not another. And on shared hosts it almost never works because it's hardly ever installed.

我喜欢 phpseclib 超过 PECL 扩展的一大优点是它是可移植的。也许 PECL 扩展适用于一个版本的 Linux 而不是另一个版本。在共享主机上,它几乎从不工作,因为它几乎从未安装过。

phpseclib is also, surprisingly, faster. And if you need confirmation that the file uploaded you can use phpseclib's built-in logging as proof.

phpseclib 也令人惊讶地更快。如果您需要确认上传的文件,您可以使用 phpseclib 的内置日志记录作为证明。

回答by Matthias

For me this worked:

对我来说这有效:

$connection = ssh2_connect($server, $serverPort);

if(ssh2_auth_password($connection, $serverUser, $serverPassword)){
    echo "connected\n";
    ssh2_scp_send($connection, "/path/to/local/".$file, "/path/to/remote/".$file);
    echo "done\n";
} else {
    echo "connection failed\n";
}

I had to install libssh2-php first, though:

不过,我必须先安装 libssh2-php:

sudo apt-get install libssh2-php

回答by Sandesh

For simple Document of phpseclib, a pure PHP SFTP implementation.

对于phpseclib 的简单文档 ,一个纯 PHP SFTP 实现

Refer Following Link:

参考以下链接:

Uploading files over SFTP using PHP

使用 PHP 通过 SFTP 上传文件

Folder Structure:

文件夹结构:

Main Folder->
    my-files(Contain File Which Transfer To Remote Server)
    phpseclib0.3.0
    sftp.php

回答by pradosh nair

Sharing further inputs, found ssh2_scp_send was not copying properly (bytes were different) when the copying the file from Linux (64 bit) to Windows (32 bit) , there sftp routine worked perfectly. When using Windows with stfp, the path in case of C:\to\path needs to be put as ssh2.sftp://{$resSFTP}/cygdrive/c/to/path if Cygwin is used for SSH on the Windows box.

共享进一步的输入,发现当将文件从 Linux(64 位)复制到 Windows(32 位)时 ssh2_scp_send 没有正确复制(字节不同),sftp 例程运行良好。使用带有 stfp 的 Windows 时,如果在 Windows 机器上使用 Cygwin 进行 SSH,则需要将 C:\to\path 中的路径设为 ssh2.sftp://{$resSFTP}/cygdrive/c/to/path .