将文件放在 SFTP 服务器上


Put a file on a SFTP Server

我想在使用公钥(不是密码(进行身份验证后将文件放在 STFP 服务器上。命令行中的手动 SFTP 有效。首先,我不能使用 phpseclib。在我的 PHP 中,"allow_url_fopen" = 1.ini

stream_get_wrappers(( 给了我: https,ftps,compress.zlib,compress.bzip2,php,file,glob,data,http,ftp,phar,ssh2.shell,ssh2.exec,ssh2.tunnel,ssh2.scp,ssh2.sftp,zip

一切正常(按顺序:ssh2_connect、ssh2_auth_pubkey_file、ssh2_sftp(但是这段代码不起作用:

$sftp = ssh2_sftp($connection);
$dest = 'test.txt';
--> $sftpStream = fopen('ssh2.sftp://'.$sftp.$dest, 'w'); <---
PHP Warning:  fopen(ssh2.sftp://Resource id #32test.txt): failed to open stream: 

知道吗?

谢谢S.

简单的错误:您忘记了$resource$dest之间的/。因此,这将起作用:

$sftpStream = fopen('ssh2.sftp://'.$sftp.'/'.$dest, 'w');

.. 前提是您对文件系统的根目录具有写入权限。您可能需要提供文件的完整路径,除了在 ssh 连接上执行cwd之外,我还没有找到在连接后将文件放入"默认"目录的方法。

phpseclib使用fsockopen,allow_url_fopen不应该影响。 allow_url_fopen 适用于 file(( 和 file_get_contents(( 之类的东西 - 不适用于 fsockopen((。