函数名:ssh2_scp_send()
适用版本:PHP 5 >= 5.1.2, PECL ssh2 >= 0.9.0
函数说明:ssh2_scp_send() 函数用于通过 SSH 协议将本地文件复制到远程服务器。
语法:bool ssh2_scp_send(resource $session, string $local_file, string $remote_file, int $create_mode = 0644)
参数:
- $session:SSH 会话资源,通过 ssh2_connect() 或 ssh2_shell() 函数返回的资源。
- $local_file:本地文件路径,需要复制到远程服务器的文件。
- $remote_file:远程服务器文件路径,文件将被复制到该路径下。
- $create_mode:可选参数,指定远程服务器上创建的文件的权限,默认为 0644。
返回值:如果成功复制文件,则返回 true,否则返回 false。
示例:
// 建立 SSH 连接
$connection = ssh2_connect('example.com', 22);
if (!$connection) {
die('Unable to establish SSH connection');
}
// 进行身份验证
if (!ssh2_auth_password($connection, 'username', 'password')) {
die('Unable to authenticate');
}
// 复制本地文件到远程服务器
$local_file = '/path/to/local/file.txt';
$remote_file = '/path/to/remote/file.txt';
if (ssh2_scp_send($connection, $local_file, $remote_file)) {
echo 'File copied successfully';
} else {
echo 'Failed to copy the file';
}
// 关闭 SSH 连接
ssh2_disconnect($connection);
以上示例中,首先使用 ssh2_connect() 函数建立与远程服务器的 SSH 连接。然后使用 ssh2_auth_password() 函数进行身份验证。接下来,通过调用 ssh2_scp_send() 函数,将本地文件 $local_file
复制到远程服务器的文件路径 $remote_file
下。最后,使用 ssh2_disconnect() 函数关闭 SSH 连接。
请注意,示例中的用户名、密码以及服务器地址应替换为实际的值。