如何从 PHP SFTP 上传文件
我在使用 PHP 通过 SFTP 将文件上传到远程服务器时遇到问题。当我使用 cURL 时,我收到此处描述的错误:
SFTP from PHP - 未定义的常量 CURLOPT_PROTOCOLS 和 CURLPROTO_SFTP?
我还按照以下建议尝试了 phpseclib:
但是当我尝试 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
http://phpseclib.sourceforge.net/documentation/intro.html#intro_usage_ Correct
据此,phpseclib 的根目录需要位于您的 include_path 中。从该页面:
您应该真正熟悉这种包含技术 - 它对于 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:
You should really familiarize yourself with this kind of include technique - it's pretty standard for PEAR libraries and Zend ones.
对于此错误,您应该在服务器的 php.ini 上安装 ssh2 扩展。
For this error, you should install ssh2 extension on your server's php.
使用内置的 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
要使用
system
命令,您必须:scp filename1 userid@hostname:filename2
等命令,选中man scp
。您可以使用sftp
执行相同的操作,但使用其他一些选项To use
system
command you have to:scp filename1 userid@hostname:filename2
, checkman scp
. You can do the same withsftp
but with some other options您可以使用 SSHv2 库的纯 PHP 实现。
以下是如何使用该库的一些示例:
以及:
You can use Pure-PHP implementation of SSHv2 library.
Here are some examples of how to use this library:
And: