php 操作sftp遍历文件
时间: 2023-07-25 13:04:29 浏览: 48
你可以使用 PHP 的 ssh2_sftp() 函数来连接到 SFTP 服务器,并使用 opendir() 和 readdir() 函数遍历文件夹。
以下是一个示例代码:
```php
// 连接到 SFTP 服务器
$connection = ssh2_connect('sftp.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$sftp = ssh2_sftp($connection);
// 遍历文件夹
$dir_handle = opendir("ssh2.sftp://{$sftp}/path/to/folder");
while ($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
echo $file . "<br>";
}
}
closedir($dir_handle);
```
要注意的是,你需要安装 PHP 的 SSH2 扩展并启用它才能使用上面的代码。