如何从 PHP SFTP 上传文件

发布于 2024-08-12 02:00:48 字数 1815 浏览 2 评论 0原文

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

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

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

SFTP from inside PHP

但是当我尝试 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

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

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

我也尝试过,

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

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

但我的 php 服务器说 ss2_connect 未定义。

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

scp file.csv [email protected]
password

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

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

关于如何从 PHP 进行 SFTP 上传的教程并不多。是因为这样做是一件坏事吗?如果是这样,我应该做什么?我要上传的服务器仅允许 sftp 连接。

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:

SFTP from PHP - undefined constant CURLOPT_PROTOCOLS and CURLPROTO_SFTP?

I also tried phpseclib as suggested in:

SFTP from within PHP

But when i try phpseclib, i get these errors:

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
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.

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

拧巴小姐 2024-08-19 02:00:48

http://phpseclib.sourceforge.net/documentation/intro.html#intro_usage_ Correct

据此,phpseclib 的根目录需要位于您的 include_path 中。从该页面:

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

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

您应该真正熟悉这种包含技术 - 它对于 PEAR 库和 Zend 库来说是相当标准的。

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:

<?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.

千笙结 2024-08-19 02:00:48

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

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

but my php server says ss2_connect is not defined.

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

晨与橙与城 2024-08-19 02:00:48

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

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

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

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

分開簡單 2024-08-19 02:00:48

要使用 system 命令,您必须:

  1. 配置无密码 ssh
  2. 运行 scp filename1 userid@hostname:filename2 等命令,选中 man scp。您可以使用 sftp 执行相同的操作,但使用其他一些选项

To use system command you have to:

  1. Configure password-less ssh
  2. Run command like scp filename1 userid@hostname:filename2, check man scp. You can do the same with sftp but with some other options
夜灵血窟げ 2024-08-19 02:00:48

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

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

<?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');
?>

以及:

<?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");
?>

You can use Pure-PHP implementation of SSHv2 library.

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");
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文