如何从 PHP SFTP 上传文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1767117/
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-25 03:46:16  来源:igfitidea点击:

How to SFTP upload files from PHP

phpsftpphpseclib

提问by John

I'm having trouble using PHP to SFTP upload files to a remote server. When I use cURL, I'm getting the error described here:

我在使用 PHP 将文件通过 SFTP 上传到远程服务器时遇到问题。当我使用 cURL 时,我收到了这里描述的错误:

SFTP from PHP - undefined constant CURLOPT_PROTOCOLS and CURLPROTO_SFTP?

来自 PHP 的 SFTP - 未定义的常量 CURLOPT_PROTOCOLS 和 CURLPROTO_SFTP?

I also tried phpseclib as suggested in:

我还按照以下建议尝试了 phpseclib:

SFTP from within PHP

PHP 中的 SFTP

But when i try phpseclib, i get these errors:

但是当我尝试 phpseclib 时,我收到了这些错误:

Warning: require_once(Math/BigInteger.php) [function.require-once]: failed to open stream: No such file or directory in /home/john/public_html/test/Net/SSH2.php on line 53

Fatal error: require_once() [function.require]: Failed opening required 'Math/BigInteger.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/john/public_html/test/Net/SSH2.php on line 53

I then tried using system commands in php like so, but nothing happened:

然后我尝试像这样在 php 中使用系统命令,但什么也没发生:

<?php
echo system('sftp [email protected]');
echo system('password');
echo system('put file.csv');
?>

I also tried

我也试过

<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);
?>

but my php server says ss2_connect is not defined.

但是我的 php 服务器说 ss2_connect 没有定义。

I tried to do the following from terminal

我尝试从终端执行以下操作

scp file.csv [email protected]
password

But the server does not allow scp command. I do not have shell access to create ssh keys.

但是服务器不允许 scp 命令。我没有创建 ssh 密钥的 shell 访问权限。

All i can do right now is sftp from terminal and manually upload. But I really want to automate this so a PHP website can do all this.

我现在能做的就是从终端 sftp 并手动上传。但我真的很想自动化这个,这样一个 PHP 网站就可以做到这一切。

There aren't many tutorials on how to SFTP upload from PHP. Is it because it's a bad thing to do? If so, what should I be doing? The server I want to upload to only allows sftp connections.

关于如何从 PHP 进行 SFTP 上传的教程并不多。是因为这样做不好吗?如果是这样,我该怎么办?我想上传到的服务器只允许 sftp 连接。

回答by dreamafter

http://phpseclib.sourceforge.net/documentation/intro.html#intro_usage_correct

http://phpseclib.sourceforge.net/documentation/intro.html#intro_usage_correct

Per that, phpseclib's root directory needs to be in your include_path. From that page:

为此,phpseclib 的根目录需要在你的 include_path 中。从该页面:

<?php
  set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');

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

You should really familiarize yourself with this kind of include technique - it's pretty standard for PEAR libraries and Zend ones.

您应该真正熟悉这种包含技术——它是 PEAR 库和 Zend 库的标准。

回答by John

but my php server says ss2_connect is not defined.

但是我的 php 服务器说 ss2_connect 没有定义。

For this error, you should install ssh2 extension on your server's php.

对于此错误,您应该在服务器的 php 上安装 ssh2 扩展。

回答by Oswaldo Rodriguez Gonzalez

You can use Pure-PHP implementation of SSHv2 library.

您可以使用 SSHv2 库的纯 PHP 实现。

Here are some examples of how to use this library:

以下是如何使用此库的一些示例:

<?php
include 'vendor/autoload.php';
$ssh = new \phpseclib\Net\SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>

And:

和:

<?php
include 'vendor/autoload.php';
$key = new \phpseclib\Crypt\RSA();
//$key->setPassword('whatever');
$key->loadKey(file_get_contents('privatekey'));
$ssh = new \phpseclib\Net\SSH2('www.domain.tld');
if (!$ssh->login('username', $key)) {
    exit('Login Failed');
}
echo $ssh->read('username@username:~$');
$ssh->write("ls -la\n");
?>

回答by Byron Whitlock

Use the built in SSH library. Using the scp function will be easier than initiating a sftp session too.

使用内置的 SSH 库。使用 scp 功能也比启动 sftp 会话更容易。

http://www.php.net/manual/en/function.ssh2-scp-send.php

http://www.php.net/manual/en/function.ssh2-scp-send.php

回答by Elalfer

To use systemcommand you have to:

要使用system命令,您必须:

  1. Configure password-less ssh
  2. Run command like scp filename1 userid@hostname:filename2, check man scp. You can do the same with sftpbut with some other options
  1. 配置无密码 ssh
  2. 像这样运行命令scp filename1 userid@hostname:filename2,检查man scp。您可以sftp使用其他选项执行相同操作