【问题标题】:How to transfer a file to the server using sftp? [duplicate]如何使用 sftp 将文件传输到服务器? [复制]
【发布时间】:2014-04-05 22:44:14
【问题描述】:

我是 PHP 新手。我有我的服务器的以下详细信息。现在我需要使用 SFTP 从 localhost 应用程序上传(或移动).sql 文件到服务器。我尝试了很多,但都失败了。

  • 主机名
  • 端口号
  • 用户名
  • 密码
  • 位置

请任何人给我一个简单的示例,将 SFTP 连接连接到我的服务器以上传文件。

【问题讨论】:

标签: php ssh sftp


【解决方案1】:

如果您只想传输文件而不关心使用scpsftp,那么我鼓励您使用scp。下面是一个基本示例 (taken from the PHP manual):

$connection = ssh2_connect('www.example.com', 22);
if($connection === FALSE) {
    die('Failed to connect');
}

$state = ssh2_auth_password($connection, 'username', 'password');
if($state === FALSE) {
    die('Failed to authenticate');
}

$state = ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);
if($state === FALSE) {
    die('Failed to transfer the file');
}

【讨论】:

  • 非常感谢您的回答。如何捕捉错误?
  • 只能看看返回值。检查我的更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 2018-07-11
  • 2019-09-01
  • 2021-12-31
相关资源
最近更新 更多