如何使用sftp将文件传输到服务器?

时间:2014-03-04 11:55:41

标签: php ssh sftp

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

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

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

1 个答案:

答案 0 :(得分:2)

如果您只是想传输文件而不关心使用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');
}
相关问题