我正在尝试: - 连接到服务器 - 列出所有文件 - 读取文件并回显内容。
连接工作良好:
$conn_id = ssh2_connect('xxx.com', 22);
$login_result = ssh2_auth_password($conn_id, 'xxxxxx', 'xxxxxxxxx');
$sftp = ssh2_sftp($conn_id);
$dir = "ssh2.sftp://$sftp/home";
$ftp_contents = scanFilesystem($dir);
然后我遍历目录并获取所有文件名,就像一个魅力:
foreach($ftp_contents as $key => $currentFilename){
echo($currentFilename);
}
然后我尝试获取文件的内容....但它不起作用。
$stream = fopen("ssh2.sftp://$sftp/home/".$currentFilename, 'r');
echo($stream);
有输出错误:
警告:fopen(ssh2.sftp://Resource id #4/home/myfile.xml) [function.fopen]:打开流失败:第 38 行 /home/xxxxxx/public_html/xxx.php 中的操作失败
第 38 行是 foreach 的结束。
我尝试:
fopen("ssh2.sftp://$sftp/home/".$currentFilename, 'r');
fopen("ssh2.sftp://$sftp/".$currentFilename, 'r');
fopen("/home/".$currentFilename, 'r');
fopen("home/".$currentFilename, 'r');
没有任何效果,总是相同类型的错误,有人可以帮助我吗?谢谢。
更新:我尝试使用:$stream = file_get_contents("ssh2.sftp://$sftp/home/$data");
也不起作用...
仍然得到错误:
警告:file_get_contents() [function.file-get-contents]: Unable to open ssh2.sftp://Resource id #3/xxx.xml on remote host in /home/xxxxxx/public_html/xxxx.php on line 40
我不知道...有人可以帮助我吗?